Vue 文件上传,IE 10 以下及谷歌低版本不支持 document.getElementById("file").files[0],文件获取不到的解决方法,
可以采用element ui 的Upload组件,参考代码如下:
<el-form id="importFileForm" label-width="140px" size="small">
<el-form-item label="选择导入的文件:">
<el-upload
class="upload-demo"
style="position:absolute;width:100px"
action="https://jsonplaceholder.typicode.com/posts/"
:before-upload="beforeupload"
multiple
:limit="1"
:http-request="httprequest"
accept=".xlsx,.xls">
<el-button icon="el-icon-upload2">上传附件</el-button>
</el-upload>
<span style="margin-left:120px">{{importFileName}}</span>
</el-form-item>
<el-form-item >
<el-button @click="submit()">提交</el-button>
</el-form-item>
</el-form>
export default {
name: "FlowImport",
data() {
return {
importFile:null,
importFileName:null,
param:new FormData(),//表单要提交的参数
};
},
methods: {
beforeupload(file){
this.importFileName = file.name;
this.param.append("file", file, file.name);
return false;
},
//覆盖默认的上传行为
httprequest() {
},
submit(){
if(this.param.get("file") === undefined || this.param.get("file") === null){
this.$message({
message: "请上传文件!",
type: 'warning'
});
return false;
}
//提交到后台
uploadIFile(this.param).then((res) => {
}
);
},
}
};
<style>
#importFile .el-upload__input{
display: none;
}
</style>