js跨域请求下载excel文件。

前后分离后

export const download = function(options){

let xhr = new XMLHttpRequest();  // XMLHttpRequest 对象xhr.open("GET", options.url, true);
xhr.setRequestHeader('Authorization', authorization());//前后分离的权限头xhr.send(obj2url(options.get));
xhr.responseType = "blob";//这里是关键,它指明返回的数据的类型是二进制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"});来下载服务器的excel文件。


叮当猫咪
浏览 1568回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript