XmlHttpRequest请求文件


function download(id) {
let url = 'http://localhost:12879/Resource/ResourceDnld?id=' + id;
let xhr=new XMLHttpRequest();
xhr.open('post',url,true);
xhr.responseType='blob';
xhr.onload=function (res) {
let filename="测试.json";
if (this.status==200){
let type=xhr.getResponseHeader('Content-Type');
let blob=new Blob([this.response],{type:type});
if (typeof window.navigator.msSaveBlob !=='undefined'){
window.navigator.msSaveBlob(blob,filename);
}
else {
let URL=window.URL || window.webkitURL;
let objectUrl = URL.createObjectURL(blob);
if (filename){
let a=document.createElement('a');
if (typeof a.download=='undefined'){
window.location=objectUrl;
}else {
a.href = objectUrl;
a.download = filename;
document.body.appendChild(a);
a.click();
a.remove()
}
}else {
window.location=objectUrl;
}
}
}
}
xhr.send();
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM