处理文件流

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
const exportContent = (url, data, fileName) => {
let token = localStorage.getItem("token");
axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
axios
.post(url, data, {
responseType: "blob", //设置返回类型
})
.then((res) => {
const link = document.createElement("a");
try {
let blob = new Blob([res.data], { type: "application/vnd.ms-excel" });
console.log(res.headers);
let _fileName = fileName;
link.style.display = "none";
const url = window.URL || window.webkitURL || window.moxURL;
link.href = url.createObjectURL(blob);
link.setAttribute(
"download",
_fileName.substring(_fileName.lastIndexOf("_") + 1)
);
link.click();
document.body.removeChild(link);
url.revokeObjectURL(link.href);
} catch (e) {
console.log("下载的文件出错", e);
}
})
.catch((err) => {
console.log("请求出错", err.response.data.error);
});
};

抛出错误的情况

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
35
36
37
38
39
40
41
42
43
44
45
46
const exportContent = (url, data, fileName) => {
let token = localStorage.getItem("token");
// axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
axios
.post(import.meta.env.VITE_ENV + url, data, {
responseType: "blob", //设置返回类型
headers: {
Authorization: `Bearer ${token}`,
},
})
.then((res) => {
const link = document.createElement("a");
res.data.text().then((res1) => {
try {
let temp = JSON.parse(res1);
ElMessage.error(temp.Message);
allLoading.value = false;
} catch (error) {
console.log(error);
try {
let blob = new Blob([res.data], {
type: "application/vnd.ms-excel",
});
console.log(res.headers);
let _fileName = fileName;
link.style.display = "none";
const url = window.URL || window.webkitURL || window.moxURL;
link.href = url.createObjectURL(blob);
link.setAttribute(
"download",
_fileName.substring(_fileName.lastIndexOf("_") + 1)
);
link.click();
document.body.removeChild(link);
url.revokeObjectURL(link.href);
allLoading.value = false;
} catch (e) {
console.log("下载的文件出错", e);
}
}
});
})
.catch((err) => {
console.log("请求出错", err.response.data.error);
});
};

处理文件流
http://ultracode.cn/2023/07/19/NewVue3/处理文件流/
作者
Win
发布于
2023年7月19日
许可协议