如何在window.location.href加请求头(token)示例导出文件
如何在window.location.href加请求头(token)示例导出文件
·
1.首先如果你的导出方式是window.location.href+url方式的话就可以
let consturl=window.config.host +"/iip/iip-faceserver/faceSystemData/downLoadExport?
url=" + response.data.content //下载地址url
var xhr = new XMLHttpRequest();
xhr.open("get", consturl, true); // get、post都可
xhr.responseType = "blob";
xhr.setRequestHeader("token",sessionStorage.getItem('token')); //加请求头
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
var farmname=consturl.substring(consturl.length-21)
a.download = farmname // 文件名
}
a.click()
window.URL.revokeObjectURL(url)
}
xhr.send();
更多推荐
已为社区贡献1条内容
所有评论(0)