一、背景

  1. post请求
  2. 调用接口A
  3. 下载excel文件不成功
  4. 谷歌浏览器Network查看调用接口A的Preview:
    在这里插入图片描述

二、解决办法

1.发送post请求时添加:responseType: 'blob'
2.调用接口后添加:

 let blob = new Blob([res.data], { type: 'application/vnd.ms-excel' });
 let url = window.URL.createObjectURL(blob);
 window.location.href = url;

3.调用接口修改excle文件名或者后缀

 let blob = new Blob([res.data], { type: 'application/vnd.ms-excel' });
 let url = window.URL.createObjectURL(blob);
 var filename = '修改的名字';
 let link = document.createElement('a')
 link.style.display = 'none'
 link.href = url
 link.setAttribute('download', filename + '.xls')
 document.body.appendChild(link)
 link.click()

参考链接:

  1. https://blog.csdn.net/ic_xcc/article/details/103632618
  2. https://blog.csdn.net/hefeng6500/article/details/82988624?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase
Logo

前往低代码交流专区

更多推荐