首先給axios設置 responseType:'blob'
下載方式:一、使用a標簽下載
axios.post(url,data,{responseType:'blob'}).then(res => {
const blob = new Blob([res.data]);//處理文檔流
const fileName = '資產列表.xlsx';
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); // 釋放URL 對象
document.body.removeChild(elink);
})
下載方式:二、使用fileDownload插件下載
git地址:https://github.com/kennethjiang/js-file-download
Axios.get(url, {
responseType: 'blob',
}).then(res => {
fileDownload(res.data, '測試.xlsx');
});