axios GET接口 文件下载
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
export const exportRecordExcel = (params) => {
return axios
.get("/boat-record/exportRecordExcel", { params, responseType: "blob" })
.then((res) => res);
};
// 设置 responseType: "blob" 不然文件打不开, 注意必须返回res,不是res.data
exportRecordExcel({
boatName: this.queryForm.boatName,
gateId: this.queryForm.gateId,
startTime: this.queryForm.startTime,
endTime: this.queryForm.endTime,
})
.then((response) => {
// const fileName = decodeURIComponent(
// response.headers["content-disposition"]
// .split(";")
// .find((n) => n.includes("filename="))
// .replace("filename=", "")
// .trim()
// ); 这边解码有点问题。。要知道编码方式?
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement("a");
link.href = url;
link.setAttribute(
"download",
`船舶信息记录表${moment().format("YYYYMMDDHHmm")}.xlsx`
);
document.body.appendChild(link);
link.click();
window.URL.revokeObjectURL(url);
})
.catch((err) => {
console.error(err);
});
This post is licensed under CC BY 4.0 by the author.