通过ajax下载文件

摘要:通过ajax方式下载文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 下载方法
function download(url, fileName) {
var xhh = new XMLHttpRequest();
xhh.open("post", url, true);
xhh.responseType = 'blob';
xhh.onreadystatechange = function () {
if (xhh.readyState === 4 && xhh.status === 200) {
var blob = xhh.response;
const url = window.URL.createObjectURL(blob);
var link = document.createElement('a');
link.href = url;
link.download = fileName;
link.click();
}
};
xhh.send();
}

// 调用方法
download('https://image.dqv5.com/public/bear.png','bear.png');