axios 当我下载一个Excel文件与Axsio有问题,但 Postman 下载正确,为什么?为什么?

ddhy6vgd  于 9个月前  发布在  iOS
关注(0)|答案(1)|浏览(110)

当我尝试下载Excel(.xlsx)文件与axios有问题,当我打开此文件:


我得到一个错误,按“是”得到以下

我已经尝试了一切,唯一的区别是,axios文件编码是UTF-8postman文件编码是ANSI
这是我的代码:

export const DownloadMonthly = (from, to) => {
  return (dispatch) => {
      return getAxios()
        .get("/monthly/download", { params: {from, to}}, 
        {
          responseType: 'arraybuffer',
          headers: {
          'Content-Type': 'application/vnd.ms-excel'
        }})
        .then((response) => { 
          const url = window.URL.createObjectURL(new Blob([response.data], {type: 'application/vnd.ms-excel'}));
          const link = document.createElement('a');
          link.href = url;
          link.setAttribute('download', "monthly.xlsx");
          document.body.appendChild(link);
          link.click();
        })
        .catch((err) => {
          console.error(err);
        }) 
  };
};
6ioyuze2

6ioyuze21#

@Zuka Gaprindashvili我认为问题在于你分配给文件的类型。
如果你正在下载一个.xlsx你会想要application/vnd.openxmlformats-officedocument.spreadsheetml.sheet你的内容类型是为.xls文件

相关问题