首页 > 解决方案 > 在 React 中下载 excel 文件大小的问题

问题描述

我正在使用 Spring-3.9.3,创建 Excel 文件并尝试从 React 下载它们。该代码适用于小文件大小(50kb),这意味着没有来自 React localhost web 的响应。

我不知道如何解决这个问题,因为我不知道错误是来自 Spring 还是 React 库。

该代码来自您可以在此处找到的教程: https ://rieckpil.de/howto-up-and-download-files-with-react-and-spring-boot/

//react code from react
class App extends Component {
  downloadExcel = () => {
    fetch('http://localhost:8080/api/files')
      .then(response => {
        const filename =  response.headers.get('Content-Disposition').split('filename=')[1];
        response.blob().then(blob => {
          let url = window.URL.createObjectURL(blob);
          let a = document.createElement('a');
          a.href = url;
          a.download = filename;
          a.click();
      });
   });
  }  
  render() {
    return (
      <div className="App-intro">
        <h3>Download an excel file</h3>
        <button onClick={this.downloadExcel}>Download</button>
      </div>
    )
  }
}

这里有我正在使用的 Spring 代码:

//spring
@RestController
@RequestMapping(method = RequestMethod.GET, value = "/api/files")
@CrossOrigin(value = {"*"}, exposedHeaders = {"Content-Disposition"})
public class FileBoundary{


    @GetMapping
    public void getEntitiesXLS(HttpServletRequest request, HttpServletResponse response) throws Exception, IOException, InvalidFormatException{     

        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Contacts");

            //create the excel file with Apache POI library

            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-Disposition", "attachment; filename=\"tickets.xlsx\"");
        workbook.write(response.getOutputStream());
    }
}

我可以在 Spring 控制台中看到请求,但在 React Web 中没有响应。有谁知道这个的解决方案?谢谢!

标签: reactjsspring-bootfrontend

解决方案


好吧,问题是微型的超时响应。


推荐阅读