import axios from “axios”;
上面引用一下axios
axios.get(“url路径”, {
params: params,
responseType: “blob”,
headers: {
Authorization: sessionStorage.getItem(‘Authorization’), //给后台传Authorization
},
})
.then((res) => {
const blob = res.data;
const href = URL.createObjectURL(
new Blob([blob], {
type:
“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”,
})
);
const a = document.createElement(“a”);
a.download = _this.fileName;//文件得名字包括后缀名
a.href = href;
document.body.appendChild(a);
a.click();//用a标签下载
document.body.removeChild(a);
})
.catch((err) => {
console.log(err);
});