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