下载文件用到了blob ,代码如下

const blob = new Blob([res]);
// const blob = new Blob([res], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }); ;
const url = window.URL.createObjectURL(blob);
const temp = document.createElement('a');// 创建a标签
temp.href = url;
temp.download = obj.fileName;
temp.click();
window.URL.revokeObjectURL(url);// 释放内存
res,是接口返回的对象,正常的应该是Blob对象吗,这时候是能正常下载的,如下
[日常坑]前端j's数据导出excel,导出的文件损坏

 

接口请求是用axios封装的。需要在接口请求中设置responseType为blob。否则res会变成如下,保存的文件就会是损坏的

[日常坑]前端j's数据导出excel,导出的文件损坏

 

 

 

 

相关文章:

  • 2021-12-11
  • 2022-12-23
  • 2021-07-18
  • 2021-06-26
  • 2022-02-12
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-23
  • 2021-06-15
  • 2022-12-23
  • 2021-10-09
  • 2021-08-05
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案