由ajax excel文件下载已损坏

我有一个Spring Boot Web应用程序,它生成扩展.xlsx Microsoft Excel文件。

如果我尝试从浏览器中下载文件,调用localhost:8080/report/stats它会返回正确的文件,我总是可以成功打开它。

但是,当我通过点击button从网页上下载一个文件,我得到一个坏的文件,我无法打开它。

我在JS上有以下部分:

 $.ajax({ url: 'report/stats', type: "GET", success: function (data) { var link = document.createElement('a'); link.download = 'report.xlsx'; link.href = 'data:,' + data; link.click(); } }); 

控制器:

 @GetMapping("stats") public ResponseEntity downloadStatsReport() throws IOException { return fileResponse(excelReportService.create(new StatFilter())); } private ResponseEntity fileResponse(File report) throws IOException { InputStreamResource resource = new InputStreamResource(new FileInputStream(report)); return ResponseEntity.ok() .contentLength(report.length()) .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + report.getName()) .contentType(MediaType.APPLICATION_OCTET_STREAM) .body(resource); } 

为什么从浏览器下载效果不错,而不是从JS的作品?

打开文件错误:

打开文件错误

YES已被点击:

在这里输入图像说明

NO有需要点击:

在这里输入图像说明

我能够使用下面的代码下载一个有效的文件

 function download(fileName) { window.location.href = "/download?description=test&logId=123"; }