原本下载文件方法(但是不能携带自定义token)
window.location.href=process.env.VUE_APP_BASE_API+"/api/exportImg?params="+encodeURIComponent(JSON.stringify(this.queryParams));
改成下面的案例就可以加上自定义Header了
我加的是Authorization
let consturl=process.env.VUE_APP_BASE_API+"/admin/order/exportImg?params="+encodeURIComponent(JSON.stringify(this.queryParams));
var xhr = new XMLHttpRequest();
xhr.open("get", consturl, true); // get、post都可
xhr.responseType = "blob";
xhr.setRequestHeader("Authorization","Bearer " + getToken()); //加请求头
xhr.onload = function() {
if(xhr.status==200){
let blob = new Blob([this.response], { type: 'application/zip' });
var a = document.createElement("a")
var url = window.URL.createObjectURL(blob)
a.href = url
a.download = 'wordOrderList' // 文件名
}
a.click()
window.URL.revokeObjectURL(url)
}
xhr.send();
效果如下