vue2+element-ui
css
<el-button
type="primary"
size="mini"
v-has="'/person/clue-import'"
@click="downLoad"
>下载模板</el-button
>
<el-button
type="primary"
size="mini"
v-has="'/person/clue-import'"
@click="onLoad"
>导入</el-button
>
<input ref="inp" type="file" style="display: none" @change="handleExcel" />
==================================================================
js
// ----下载模板
downLoad() {
axios({
method: "GET", //或者POST
url: "leads-api/talentleads/exportTalentMould",
responseType: "blob",
})
.then((res) => {
let result = res.data;
let blob = new Blob([result], { type: "application/vnd.ms-excel" });
let url = window.URL.createObjectURL(blob);
let link = document.createElement("a");
link.download = "人才线索导入模版.xls";
link.href = url;
link.click();
})
.catch((err) => {
console.log(err);
});
},
// 点击导入同步input点击事件
onLoad(){
this.$refs.inp.click()
},
//导入
handleExcel(e) {
console.log(e);
let params = e.target.files[0];
if (params != undefined) {
var param = new FormData();
param.append("file", params);
if (params.type != "application/vnd.ms-excel") {
this.$message({
title: "警告",
message: "上传格式不正确,请上传xls或者xlsx格式",
type: "warning",
});
return false;
}
let size = params.size / 1048576;
size = size.toFixed("2");
let type = params.type;
if ((type == "xls" && size > 10) || (type == "xlsx" && size > 10)) {
this.$message({
title: "警告",
message: "图片不能超过10M",
type: "warning",
});
return false;
}
let config = {
headers: { "Content-Type": "multipart/form-data" },
}; //添加请求头
let url = "leads-api/talentleads/importLeads";
axios.post(url, param, config).then((res) => {
if(res.data.code===0){
if(res.data.result.success){
this.$message.success('导入成功'+res.data.result.success+'条')
}else if(res.data.result.err){
this.$message.error('导入失败'+res.data.result.err+'条')
}
this.$refs.inp.value=''
this.getOrderList();
}
});
}
},