发送下载文件的请求接后端的文件流

// 发送下载的请求
function downLoadByUrl(url){
var xhr = new XMLHttpRequest();
//GET请求,请求路径url,async(是否异步)
xhr.open('GET', url, true);
//设置请求头参数的方式,如果没有可忽略此行代码
xhr.setRequestHeader("token", cookieToken);
xhr.setRequestHeader("userId", userId);
//设置响应类型为 blob
xhr.responseType = 'blob';
//关键部分
xhr.onload = function (e) {
//如果请求执行成功
if (this.status == 200) {
let blob = this.response;
let filename = "XX信息-"+new Date()+".xlsx";//如123.xls
let a = document.createElement('a');
blob.type = "application/octet-stream";
//创键临时url对象
let url = URL.createObjectURL(blob);
a.href = url;
a.download=filename;
a.click();
//释放之前创建的URL对象
window.URL.revokeObjectURL(url);
}
};
//发送请求
xhr.send();
}
上一篇:Oracle中REGEXP_SUBSTR函数(转)


下一篇:如何将word公式粘贴到xhEditor里面