请求下载文件

function handleDownloadFile(url) {
const fileName = url.split('/').pop().split('?')[0];
var xhr = new XMLHttpRequest();
xhr.open('get', url, true);
xhr.responseType = 'blob';
xhr.onload = function() {
if (this.status === 200) {
const data = this.response;
const url = window.URL.createObjectURL(data)
var a = document.createElement('a');
a.download = fileName;
a.href = url;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}
}
xhr.send();
}

你可能感兴趣的:(请求下载文件)