方法1:XMLHttpRequest
downloadXlsFile(url,token,name){
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('token', token);
xhr.responseType = "blob";
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
// 数据在 this.response 保存
// excel 的 MIME 格式为 application/vnd.ms-excel
var blob = new Blob([this.response], {type: "application/vnd.ms-excel"});
console.log(this.response)
// 创建a链接 href链接地址 download为下载下来后文件的名称
var aa = document.createElement('a');
aa.href = URL.createObjectURL(blob);
aa.innerHTML = 'a链接';
aa.download = name;
aa.style.display = 'none'; //隐藏a标签 直接调用a标签的点击事件
document.body.appendChild(aa);
aa.click();
} else {
//文件流结束关闭loading加载框
}
}
xhr.send();
}
方法2
authExcelEnvDownload(this.content).then(res=>{this.downloadData(res.data)})
downloadData(data){
const url = window.URL.createObjectURL(new Blob([data], { type: 'application/vnd.ms-excel' }))
const link = document.createElement('a')
link.href = url
let attributeValue = `信息反馈.xls`
link.setAttribute('download', attributeValue)
document.body.appendChild(link)
link.click()
link.remove()
this.$notify({title:'提示',message:'下载即将开始...'})
},
export function authExcelEnvDownload(data) {
return request({
url: `/download`,
method: 'post',
data,
responseType: 'blob' // 重点在于配置responseType: 'blob'
})
}