首页 > 解决方案 > 下载excel无法打开文件,因为文件格式或文件扩展名

问题描述

const data = ('start_date=2019-07-04&end_date=2019-07-24');
    axios({
      url: `http://localhost:4000/report/data?${data}`,
      method: 'GET',
      responseType: "arraybuffer", // important
    }).then(response => {
      // BLOB NAVIGATOR
      let blob = new Blob([response.data], { type: '' });

      if (navigator.msSaveOrOpenBlob) {
        navigator.msSaveBlob(blob);
      } else {

        let link = document.createElement('a');
        link.href = window.URL.createObjectURL(blob);
        link.setAttribute('download', '');
        document.body.appendChild(link);
        link.download = [];
        link.click();
        document.body.removeChild(link);
      }
    });

我正在尝试使用 reactJS 下载 xlsx 文件,但是当我在下载后尝试打开文件时收到此消息:

“Excel 无法打开文件‘file.xlsx’,因为文件格式或文件扩展名无效。验证文件未损坏且文件扩展名与文件格式匹配”

这是前端代码:

标签: javascriptreactjs

解决方案


推荐阅读