VUE的axios实现excel导出下载功能
//添加axios拦截器axios.interceptors.response.use(function(res){if (res.headers && (res.headers['content-type'] === 'application/x-msdownload' || res.headers['content-type'] === 'application
·
//添加axios拦截器
axios.interceptors.response.use(function(res){
if (res.headers && (res.headers['content-type'] === 'application/x-msdownload' || res.headers['content-type'] === 'application/vnd.ms-excel')) {
downloadUrl(res.request.responseURL)
return {data:{rCode:'success'}}
}
return res;
},function(err){
//Do something with response error
return Promise.reject(error);
})
//使用iframe框架下载文件
const downloadUrl = url => {
let iframe = document.createElement('iframe')
iframe.style.display = 'none'
iframe.src = url
iframe.onload = function () {
document.body.removeChild(iframe)
}
document.body.appendChild(iframe)
}
缺点:导出数据时Controller的查询方法会执行两次
更多推荐
已为社区贡献1条内容
所有评论(0)