使用axios.get获取后端传来的数据流,前端用blob接收需要吧blob跟传参放一起,之前是分开放,一点错没有,就是打不开Excel文件,真是麻了,主要还是eslint好像是关了没有报错。代码如下:
axios
.get(
"http://localhost:8989/stayregister/exportExcel",{
params,
responseType:'blob',
}
)
.then((res) => {
console.log(res);
var temp = res.headers["content-disposition"]
.split(";")[1]
.split("filename=")[1];
var fileName = decodeURIComponent(temp);
const blob = new Blob([res.data], {
type: "application/vnd.ms-excel;charset=UTF-8",
});
if ("download" in document.createElement("a")) {
// 非IE下载
const a = document.createElement("a");
a.download = fileName;
a.style.display = "none";
a.href = URL.createObjectURL(blob);
document.body.appendChild(a);
a.click();
URL.revokeObjectURL(a.href); // 释放URL 对象
document.body.removeChild(a);
} else {
// IE10+下载
navigator.msSaveBlob(blob, fileName);
}
8;
})
.catch((err) => {
console.error(err);
});