通过Ajax下载文件

通过Ajax下载文件

参考网站https://send.firefox.com/ Github

1
<button type="button" id="GetFile">Get File!</button>
file_download_via_ajax.jsview raw
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
$('#GetFile').on('click', function () {
$.ajax({
url: 'https://kekek.cc/static/P60524-122812.jpg',
xhr: function () {
var xhr = $.ajaxSettings.xhr();
xhr.onprogress = function (e) {
// For downloads
if (e.lengthComputable) {
console.log(e.loaded / e.total);
}
};
xhr.upload.onprogress = function (e) {
// For uploads
if (e.lengthComputable) {
console.log(e.loaded / e.total);
}
};
return xhr;
},
method: 'GET',
xhrFields: {
responseType: 'blob'
},
success: function (data) {
var a = document.createElement('a');
var url = window.URL.createObjectURL(data);
a.href = url;
a.download = 'P60524.jpg';
a.click();
window.URL.revokeObjectURL(url);
}
});
});

参考

本站采用「署名 4.0 国际」进行许可。