vue+axios文件流导出文件

导出代码:
this.$http.get(`url`,null)
.then(response => {
// console.log(response);
if (!response) {
return
}
let url = window.URL.createObjectURL(new Blob([response.data],{ type: 'application/vnd.ms-excel' }))
let link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('download', fileName)
document.body.appendChild(link)
link.click()
window.URL.revokeObjectURL(link.href);
document.body.removeChild(link)
})
.catch(error => { });

请求拦截器里面设置axios的请求类型为blog,responseType为blob。

响应拦截器:因为返回的文件数据和之前请求的接口数据冲突,特殊处理了一下。

if(reqUrl.indexOf('exportDRankScoreInfo') > -1){
//导出文件
return Promise.resolve(response);
}

 

 



posted @ 2021-01-18 14:26  敲敲碰碰  阅读(337)  评论(0编辑  收藏  举报