//添加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的查询方法会执行两次

Logo

前往低代码交流专区

更多推荐