美文网首页工作生活
请求下载文件

请求下载文件

作者: 放任自由f0 | 来源:发表于2019-07-03 13:05 被阅读0次

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();
}

相关文章

网友评论

    本文标题:请求下载文件

    本文链接:https://www.haomeiwen.com/subject/iwsqhctx.html