# web 中文件下载
直接是 url 的下载
export function downloadURL(url, name = '') {
const link = document.createElement('a')
link.download = name
link.href = url
if ('download' in document.createElement('a')) {
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
} else {
// 对不支持download进行兼容
click(link)
}
}
// clone https://github.com/eligrey/FileSaver.js/blob/master/src/FileSaver.js
function click(node) {
try {
node.dispatchEvent(new MouseEvent('click'))
} catch (e) {
var evt = document.createEvent('MouseEvents')
evt.initMouseEvent(
'click',
true,
true,
window,
0,
0,
0,
80,
20,
false,
false,
false,
false,
0,
null
)
node.dispatchEvent(evt)
}
}
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
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
如果我们得到的二进制文件流
// 我们通过设置 {responseType: 'arraybuffer'}
export async function downloadFile(blob) {
return new Promise(async (resolve, reject) => {
try {
const objectUrl = URL.createObjectURL(blob)
downloadURL(objectUrl, fileName)
// 手动标记用于内存回收,防止内存泄漏
URL.revokeObjectURL(url)
resolve(true)
} catch (err) {
console.error(err)
reject(err)
}
})
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16