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

下载文件用到了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对象吗,这时候是能正常下载的,如下

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

 
原文地址:https://www.cnblogs.com/m7777/p/14681005.html