下載文件用到了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會變成如下,保存的文件就會是損壞的