下载 post请求 携带token
作者:互联网
downOut () {
//获取地址
const env = process.env.VUE_APP_ENV
let url = config.baseUrl[env] + "Device/ExportDevice"
//创建请求
const req = new XMLHttpRequest();
//请求体 JSON格式 其他格式参考:https://blog.csdn.net/harryhare/article/details/80778066
let formData = JSON.stringify(this.selectedRowKeys);
req.open('POST', url, true);
req.responseType = 'blob';
req.setRequestHeader('Content-Type', 'application/json');
//请求携带token
req.setRequestHeader('token', localStorage.getItem('sysToken'))
req.onload = () => {
const data = req.response;
const a = document.createElement('a');
const blob = new Blob([data], { type: "application/octet-stream" });
const blobUrl = window.URL.createObjectURL(blob);
this.download(blobUrl);
};
req.send(formData);
},
download (blobUrl) {
const a = document.createElement('a');
a.style.display = 'none';
a.download = 'xxx.xls'; //文件名
a.href = blobUrl;
a.click();
// document.body.removeChild(a);
},
标签:blobUrl,const,携带,req,token,env,download,post,document 来源: https://blog.csdn.net/anitem/article/details/122435230