接口返回的类型是文件流的格式
前端:
//前端封装的接口
//请求接口时,请求类型设置为blob;responseType:'blob';
//responseType设为blob的话,返回的文件流将会被转成blob对象,不是文件流
export function exportlive(par) {
return service({
method: 'GET',
url: 'exporting_live',
params: par,
headers: {
'Access-Sign': signature,
'Access-Time': timestamp,
'Access-Device': deviceObj.device,
'Access-Version': version,
'Content-Type': ' application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
},
responseType: 'blob'
})
}
页面中请求
getexportlive() {
exportlive({
liveId: this.$route.query.id,
type: this.formState.type || '0',
site: this.formState.site || '',
username: this.formState.username || '',
comnpanyName: this.formState.comnpanyName || ''
}).then((res) => {
var fileName = '.xlsx'
fileName = '表格' + fileName
let blob = new Blob([res.data], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
})
var url = window.URL.createObjectURL(blob);
var aLink = document.createElement("a");
aLink.style.display = "none";
aLink.href = url;
aLink.setAttribute("download", fileName);
document.body.appendChild(aLink);
aLink.click();
document.body.removeChild(aLink); //下载完成移除元素
window.URL.revokeObjectURL(url); //释放掉blob对象
})
.catch(function (err) {
console.log(err);
this.$message.warning('导出失败!')
});
}```
最终结果 导出成功
如有不成功者 可以看一下 网上找的 https://www.jianshu.com/p/bed4e8ae10ba
|