无法用Angular JS和spring MVC打开下载文件

我在我的网站上实现了一个function,并使用Angular JS进行操作。 我已经实现了一个服务器端 导出文件服务 。 在客户端,我创build了一个“ EXPORT OPTION ”,然后当用户点击那个button时,用户可以下载文件(从我的服务端导出),并自动保存到本地机器的下载文件夹中。 我从这个问题的“tremendows”的答案中得到了这个想法: 如何使用AngularJS下载文件并调用MVC API?

一切都做得很好,但是,当我打开保存到下载文件夹中的文件,显示错误信息:“” Excel无法打开文件'filename.xlsx',因为文件扩展名的文件格式无效。 确认文件没有被损坏,并且文件的扩展名与文件的格式相匹配 。“

我相信问题是从客户端出来的,因为当我在服务端运行服务(用于导出文件)时,文件被创build并正确打开,没有任何错误。 我一直在调查这个问题,然后我发现问题应该从文件path“.xlsx”中出来。

这里是在服务器端导出文件的java代码:

@RequestMapping(value = "/exportFile", method = RequestMethod.GET) public String exportHRSalaryByAcc(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "month", defaultValue = "") String month) throws IOException, GeneralSecurityException, InvalidFormatException{ String rs = FALSE; response.addHeader("Access-Control-Allow-Origin", "*"); response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); //response.setContentType("application/x-felix; charset=us-ascii"); response.setHeader("Content-Transfer-Encoding", "7bit"); String fName = null; Calendar cal = Calendar.getInstance(); int m = 0; try { if(StringUtils.isNotEmpty(String.valueOf( month)) || StringUtils.isNotEmpty(String.valueOf( year))){ if (DateUtils.isValidFormatver2(Integer.parseInt(month), Integer.parseInt(year))) { m = Integer.parseInt(month); fName = "C:\\LOCAL\\FILE_" + "[" + m + "]" + "[" + y + "]" + ".xlsx"; // CREAT FILE rs = eService.runExportDataHRSalay(m, fName); } else { return ErrorMessage.FILE_MESS; } } else { m = cal.get(Calendar.MONTH) + 1; fName = "C:\\LOCAL\\FILE_" + "[" + m + "]" + ".xlsx"; rs = eService.runExportDataHRSalay(m, fName); } } catch (Exception e) { e.printStackTrace(); } response.setHeader("Content-Disposition", "attachment; filename=\"" + fName); return rs; } 

这里是客户端的代码,我用的是angularjs:

  ..... month = currentTime.getMonth() + 1; var check = confirm("Are you sure to run the data ?"); if(check == true){ $http({ url : www.serviceFromServerSide.abc/exportFile, method : 'GET', headers : { 'Content-type' : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', }, responseType : 'arraybuffer' }).success(function(data, status, headers, config) { var file = new Blob([ data ], { type : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }); //trick to download store a file having its URL var fileURL = URL.createObjectURL(file); var a = document.createElement('a'); a.href = fileURL; console.dir(fileURL); a.target = '_blank'; a.download = 'FILE__' + '[' + month + ']'+ '.xlsx'; document.body.appendChild(a); a.click(); $("#loadingImage").css("display", "none"); $("#backgroundPopup").fadeOut(400); }).error(function(data, status, headers, config) { }); }else{}.......... 

请帮我弄清楚,我真的因为这个而筋疲力尽。 谢谢大家,对于糟糕的解释感到抱歉。

我有同样的问题,我解决它.xlsxreplace.xlsx,基本上:

response.addHeader(“Content-Disposition”,“attachment; filename = report.xls”);

在你的代码中,replace这行代码:

 fName = "C:\\LOCAL\\FILE_" + "[" + m + "]" + "[" + y + "]" + ".xlsx"; 

附:

 fName = "C:\\LOCAL\\FILE_" + "[" + m + "]" + "[" + y + "]" + ".xls";