下载文件的处理
直接a标签处理
<a style="color:#fff" href="<下载链接>" >导出</a>
js处理可添加token处理:
let elink = document.createElement(‘a’);
elink.download = “文件名字.xls”;
elink.href = ${url}?token=${token}&${hash}
;
elink.click();
fetch blob处理
// let newOptions = {
// loginName: localStorage.getItem('username') || '',
// 'Content-Type': 'application/form-data; charset=utf-8',
// token,
// }
// fetch("/api/insurance/customer/export?begintime=2016-07-01&endtime=2019-07-01&store_id=", { responseType: 'blob'})
// .then(response => {
// // 注意,该处并非response.json()
// return response.blob();
// })
// .then(res => {
// var blob = new Blob([res]);
// let elink = document.createElement('a');
// console.log(blob)
// elink.download = "download.xls";
// elink.href = URL.createObjectURL(blob);
// elink.click();
// })
// .catch(e => {
// console.log('error:' + e)
// })
http处理
function download(filename) {
let oReq = new XMLHttpRequest();
oReq.open("GET", "<你的返回流的Action路径>", true);
oReq.responseType = "blob";
// 设置验证密匙
oReq.setRequestHeader("token",token);
oReq.setRequestHeader("loginName",localStorage.getItem('username') || '');
oReq.onload = function () {
var content = oReq.response;
var blob = new Blob([content]);
// 创建a标签,下载处理过的文件流
var elink = document.createElement('a');
elink.download = filename;
elink.href = URL.createObjectURL(blob);
elink.click();
};
oReq.send();
}
axios 下在处理
注意返回值你是否axios统一封装,不然会报错
this.$http("/api/insurance/customer/export?begintime=2016-07-01&endtime=2019-07-01&store_id=", { responseType: 'blob'})
.then(response => {
// 注意,该处并非response.json()
return response.blob();
})
.then(res => {
var blob = new Blob([res]);
let elink = document.createElement('a');
console.log(blob)
elink.download = "download.xls";
elink.href = URL.createObjectURL(blob);
elink.click();
})
.catch(e => {
console.log('error:' + e)
})