在React中下载Excel文件大小的问题

时间:2019-05-18 09:02:30

标签: reactjs spring-boot frontend

我正在使用Spring-3.9.3,创建Excel文件并尝试从React下载它们。该代码可以在文件大小较小(50kb)的情况下正常工作,这不仅意味着React本地主机网络没有响应。

我不知道如何解决该问题,因为我不知道错误是来自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中没有响应。有人知道解决方案吗?谢谢!

1 个答案:

答案 0 :(得分:1)

嗯,问题出在微控制器的超时响应上。