廢話少說卒废,上code:
axiosFunc({
responseType: 'blob',
headers: {
'Content-Type': 'application/json;charset=utf-8'
}
...otherOptions
})
.then((res: any) => {
const stream: any = res.data; // 后端用stream返回Excel文件
const blob: any = new Blob([stream]);
if (res.status !== 200) {
// 非正常業(yè)務碼策治,執(zhí)行錯誤處理
// 注意:status字段名是團隊自行定義的
}
// 前端獲取業(yè)務碼塑煎,成功執(zhí)行正常業(yè)務
const downloadElement = document.createElement('a');
const href = window.URL.createObjectURL(blob); // 創(chuàng)建下載的鏈接
downloadElement.href = href;
const timeStamp = new Date().toString();
const nameStr = timeStamp;
downloadElement.download = nameStr; // 下載后文件名
document.body.appendChild(downloadElement);
downloadElement.click(); // 點擊下載
document.body.removeChild(downloadElement); // 下載完成移除元素
window.URL.revokeObjectURL(href); // 釋放掉blob對象
});
}