利用FormData对象进行下载文件

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
const formData = new FormData();
formData.append('参数1', 参数1);
formData.append('参数2', 参数2);
formData.append('参数3', 参数3);
formData.append('参数4', 参数4);
formData.append('参数5', 参数5);
//...............以此类推
fetch(`/api${PREFIX_URL}/export`, {
method: 'POST',
body: formData,
headers:{
'token':store.state.token,
//headers携带的各种数据
}
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.blob(); // 将响应转换为 Blob 对象
})
.then(blob => {
// 创建一个链接元素
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = this.projectInfo.projectName+'.'+(this.exportInfo.fileType==='1'?'xlsx':'csv'); // 设置下载的文件名
document.body.appendChild(a);
a.click(); // 触发下载
a.remove(); // 移除链接元素
window.URL.revokeObjectURL(url); // 释放 URL 对象
})
.catch(error => {
console.error('There was a problem with the fetch operation:', error);
});

利用FormData对象进行下载文件
http://ultracode.cn/2025/02/26/NewVue2/利用FormData对象进行下载文件/
作者
Win
发布于
2025年2月26日
许可协议