方式一:
this.postExcelFile(res.data.data,`${base.baseUrl}/sys/recordTemplate/exportExcel`);
第一个参数是 文件流 res.data.data
第二个参数是下载地址 `${base.baseUrl}/sys/recordTemplate/exportExcel`
postExcelFile(params, url) {
// params是post请求需要的参数,url是请求url地址
const form = document.createElement('form');
form.style.display = 'none';
form.action = url;
form.method = 'post';
document.body.appendChild(form);
const input = document.createElement('input');
input.type = 'hidden';
input.name = 'idList';
input.value = params;
form.appendChild(input);
form.submit();
form.remove();
},
完整代码:
this.$api.paramsConfig.checkExportExcelTemplate(ids).then(res => {
return res;
}).then((res)=>{
const h = this.$createElement
let stringResult = res.data.message.split(';');
let arr = []
stringResult.forEach(item => {
let _t = h('p',item)
arr.push(_t)
});
this.$confirm('提示', {
title: '提示',
message: h('div', arr),
confirmButtonText: '继续下载',
cancelButtonText: '取消'
}).then(() => {
this.postExcelFile(res.data.data,`${base.baseUrl}/sys/recordTemplate/exportExcel`);
}).catch(() => {
this.$message({
type: 'info',
message: '已取消操作',
duration: 1500
});
});
}).catch((err)=>{
let stringResult = err.data.message.split(';');
let arr = []
stringResult.forEach(item => {
let _t = item+'<br />'
arr.push(_t)
});
this.$alert(arr.join().replace(/,/,''), '信息提示', {
dangerouslyUseHTMLString: true
});
})
},
postExcelFile(params, url) {
// params是post请求需要的参数,url是请求url地址
const form = document.createElement('form');
form.style.display = 'none';
form.action = url;
form.method = 'post';
document.body.appendChild(form);
const input = document.createElement('input');
input.type = 'hidden';
input.name = 'idList';
input.value = params;
form.appendChild(input);
form.submit();
form.remove();
},
方式二:
this.$api.lxtinitial.download(params).then(res => {
if(res.data.respCode == 0){
const url = base.wang +'/file'+ res.data.data;
const a = document.createElement('a');
a.setAttribute('href', url);
a.click();
}else{
this.$message.error({message: res.data.message, duration: 1500});
}
})