reactjs - 在 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 中没有响应。有谁知道这个的解决方案?谢谢!
解决方案
好吧,问题是微型的超时响应。
推荐阅读
- c++ - 无法弄清楚双变量
- c# - NSubstitute 模拟一个没有参数的方法以进行多次调用
- css - 如何按预期设置垫选择高度和宽度?
- angular - 什么时候在 Angular 中使用自定义指令有用?
- android - 重复类错误“重复类 com.google.android.gms.internal.vision.zzbl”
- vim - 如何在vim中返回到最后一个光标位置
- r - 如果缺少工作表,则需要跳过循环中的 Read.XLS 多个文件
- android - 从cordova android应用程序中删除启动画面
- excel - 使用 Excel 表单创建多行
- node.js - 如何不在 Node.js 中等待完成子进程?