手动上传excel和上传图片类似,只是格式不一样
html:
<el-upload
class="upload-demo inline-block"
action="string"//随便填
size="mini"
accept=".xls, .xlsx"//限制上传类型
:http-request="httpRequest"
:file-list="showExcelist"//上传文件list
:before-upload="beforeAvatarUpload"
>
<el-button size="small">选择文件</el-button>
</el-upload>
<el-button size="mini" type="primary" @click="saveexcel">保存</el-button>
js:
//上传前的操作,获取到上传excel的file
httpRequest(params) {
this.excelFile = params;
let formdata = new FormData();
formdata.append("File", params.file);
formdata.append("FileName", params.file.name);
},
beforeAvatarUpload(file) {
//格式过滤
const isXls =
file.type === "application/vnd.ms-excel"
? true
: file.type ===
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
? true
: false;
if (!isXls) {
this.$message.error("上传的文件只能是xls以及xlsx格式!");
} else {
this.excelFile = file;//获取到file 单独存一下
}
return isXls;
},
//走接口,传给后端
saveexcel() {
if (this.excelFile.file) {
let formdata = new FormData();//传参格式手动改为fromdata
let params = this.excelFile;
formdata.append("batchFile", params.file);
formdata.append("FileName", params.file.name);
importSaledetail(formdata).then(res => {//请求后端,自己封装的axios
if (res.rtnCode == 200) {//成功
this.$notify({
title: "成功",
message: res.msg,
type: "success"
});
this.excelFile = []; //成功后参数置空
this.showExcelist = [];//成功后上传文件list置空
} else {//失败
this.$message.error(res.msg);
}
});
} else {
this.$message.error("请选择文件!");
}
},