当前位置: 首页 > 知识库问答 >
问题:

反应 excel 文件下载损坏

桓瀚
2023-03-14

我试图通过调用Spring RESTendpoint在Reactjs中下载Excel文件,但我遇到了损坏文件的问题。

回应呼叫...

getFile(){
    axios.get('get/download')
        .then((response) => {
            var blob = new Blob([response.data], {type:'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
            filesaver.saveAs(blob, "excel.xlsx");
        });
}

Spring控制器……

@RequestMapping(value = "/download", method = RequestMethod.GET)
    public void downloadExcelFile(final HttpServletResponse response) throws IOException {
        response.setHeader("Content-Encoding", "UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename="file.xlsx");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

        final File xls = service.createExcelFile(response);

        final FileInputStream in = new FileInputStream(xls);
        final OutputStream out = response.getOutputStream();

        final byte[] buffer = new byte[8192];
        int length;

        while ((length = in.read(buffer)) > 0) {
            out.write(buffer, 0, length);
        }
        in.close();
        out.close();
    }

服务

 public File createExcelFile(final HttpServletResponse response) {
     XSSFWorkbook xssfWorkbook = null;
     final File xls = new File("excel.xlsx");
        try {
            final FileOutputStream fos = new FileOutputStream(xls);

            xssfWorkbook = new XSSFWorkbook();
            //setup excel file...
            xssfWorkbook.write(fos);
            xssfWorkbook.close();
       }
       catch (final Exception e) {
           LOGGER.error(String.format("Something went wrong"));
       }
    return xls;
 }

当我执行上面的代码,我得到一个Excel文件o. k……但看response.data看起来像……

"PKs��J_rels/.rels���j�0��}↵�{㴃1F�^Ơ�2��l%1I,c�[�

嘎嘎��由于数据不可读,无法打开文件。打开服务器上创建的文件是可以的

欢迎任何想法

干杯

共有1个答案

韩彦君
2023-03-14

您只需向axios请求添加响应类型:

responseType: 'arraybuffer'

在我的应用程序中,以下函数下载Excel文件:

function exportIssues() {
  axios.get('/issues/export', { responseType: 'arraybuffer' })
    .then((response) => {
      var blob = new Blob([response.data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
      fileSaver.saveAs(blob, 'fixi.xlsx');
    });
}
 类似资料:
  • 我使用Laravel创建了一个Restful API,该API如下: 这是我的下载代码: 它在邮递员中工作,也在浏览器中工作,直接下载文件,但使用react。js,它不工作,这是我在react中的代码: 我在按钮中调用这个函数,如下所示: id已更正,我由此确定,我的问题是单击此按钮时如何下载文件。。丹。。。

  • 问题内容: 我以前使用Axios下载GET端点提供的文件。端点已更改,现在是POST,但是不需要参数。我正在更新原始的下载方法,但是返回了损坏的文件。 我不知道,如果问题出在,或如何响应的处理或全部的上方。到目前为止,我已经尝试了各种选择,但没有运气。任何建议将不胜感激! 我已经能够使用Postman下载文件,所以我知道端点提供的文件很好。我只是无法在我的React代码中理清参数来做到这一点。 问

  • 我编写的下载文件的方法总是产生损坏的文件。 我通过adb访问这些文件,将它们传输到我的sccard,在那里我看到它们似乎有合适的大小,但没有根据例如Linux命令的类型。 你知道丢失了什么以及如何修复它吗? 谢谢。 代码的简单版本(但错误相同) 日志:< code > file . length:2485394 | content length:1399242 问题是,我从我的API单例中获得了,

  • 我正在使用Postman测试一个视图,该视图应该允许用户下载excel文件。这是视图。。 问题是,当我测试这个视图时,我无法从postman那里获得下载提示。相反,我从邮递员的回复中得到了损坏的数据。以下是一个示例输出: 为什么我不能下载文件?我错过了什么吗?

  • 问题内容: 我正在使用角度$ http从服务器下载文件。文件类型可以不同。我应该设置请求标头以进行身份​​验证。下载完成后,文件已损坏!这是我在客户端保存文件的代码: 问题答案: 我最终通过将以下配置添加到ajax请求中解决了该问题: 并将Blob类型更改为 “应用程序/八位字节流”

  • 我想把一个示例xlsx文件放在我的公用文件夹(react项目)中,其他人可以下载这个文件。 我该怎么做?