后端返回的excel 文件流
场景: 如果失败则返回JSON 数据 、如果成功则直接返回文件流
所以有try catch 包裹
reqHandle.exportCategorySubrecord(params)
.then(res => {
setIsLoading(isLoading => !isLoading)
if (!res) return;
try {
let enc = new TextDecoder('utf-8')
res = JSON.parse(enc.decode(new Uint8Array(res)))
if (res.code !== "0") {
message.warning(res.message)
}
} catch (err) {
const content = res;
const blob = new Blob([content]);
let url = window.URL.createObjectURL(blob);
let link = document.createElement("a");
link.style.display = "none";
link.href = url;
link.download = `分类报备明细${moment().format("YYYY/MM/DD HH:mm:ss")}.xlsx`
document.body.appendChild(link);
link.click();
}
})
.catch(err=>{
setIsLoading(isLoading => !isLoading)
})