前后分離后export const download = function(options){let xhr = new XMLHttpRequest(); // XMLHttpRequest 對象xhr.open("GET", options.url, true);
xhr.setRequestHeader('Authorization', authorization());//前后分離的權(quán)限頭xhr.send(obj2url(options.get));
xhr.responseType = "blob";//這里是關(guān)鍵,它指明返回的數(shù)據(jù)的類型是二進制xhr.onreadystatechange = function(e) {
if (this.readyState == 4 && this.status == 200) { console.log('change download');
var response = xhr.response; let url = URL.createObjectURL(response);
img.src=url;//如果這里是圖片,可以顯示出圖片。
window.open(url);//想通過打開窗口來下載excel文件。卻不可以。
}
}};想通過download({url:"http://xxx.ssss:88/sss.xls"});來下載服務(wù)器的excel文件。
js跨域請求下載excel文件。
叮當(dāng)貓咪
2018-10-11 13:19:01