axios 文件流导出excel
一 . 设置responseType 为 arraybuffer
axios.post(`${origin}/baisp_appointment/api/plat/xxx`, params, {
responseType: 'arraybuffer',
headers: { token: getToken(), account: getAccount() },
}).then((res) => {
let fileName = format(new Date(), 'yyyy-MM-dd');
this.exportSearchList(res.headers['content-disposition'].split('=')[1], res.data);
}).catch();
二.处理数据流导出文件
exportSearchList(dowLoadFileName, result) {
let blob = new Blob([result]);
const fileName = dowLoadFileName;
var brower = '';
if (navigator.userAgent.indexOf('Edge') > -1) {
brower = 'Edge';
}
if ('download' in document.createElement('a')) {
if (brower == 'Edge') {
navigator.msSaveBlob(blob, fileName);
return;
}
const elink = document.createElement('a');
elink.download = fileName;
elink.style.display = 'none';
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
URL.revokeObjectURL(elink.href);
document.body.removeChild(elink);
} else {
navigator.msSaveBlob(blob, fileName);
}
},