使用XMLHttpRequest下载文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const xhr = new XMLHttpRequest()
xhr.open('get', '下载链接')
xhr.responseType = 'blob'
xhr.onreadystatechange = function () {
if (xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
saveAs(xhr.response, '文件名称')
}
}
xhr.send();

function saveAs(data, name) {
var urlObject = window.URL || window.webkitURL || window;
const export_blob = new Blob([data]);
var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a')
save_link.href = urlObject.createObjectURL(export_blob);
save_link.download = name;
save_link.click();
}