[英]Angular: can't download server side generated spreadsheet
I'm trying to download a server generated spreadsheet. 我正在尝试下载服务器生成的电子表格。
My application uses Angular on the front-end and Java on the back-end. 我的应用程序在前端使用Angular,在后端使用Java。
Here's the method on the back-end that receives the request to generate and return the file: 这是后端上接收生成和返回文件请求的方法:
@RequestMapping(value = "download", method = RequestMethod.GET, produces = "application/xls")
public ResponseEntity<InputStreamResource> download() throws IOException {
ByteArrayOutputStream fileOut = new ByteArrayOutputStream();
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet worksheet = workbook.createSheet("POI Worksheet");
HSSFRow row1 = worksheet.createRow((short) 0);
HSSFCell cellA1 = row1.createCell((short) 0);
cellA1.setCellValue("Hello!");
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFillForegroundColor(HSSFColor.GOLD.index);
cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cellA1.setCellStyle(cellStyle);
workbook.write(fileOut);
fileOut.close();
byte[] file = fileOut.toByteArray();
return ResponseEntity
.ok()
.contentLength(file.length)
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(new InputStreamResource(new ByteArrayInputStream(file)));
}
And on the front-end, the following function is executed when the user clicks on Download button: 在前端,当用户单击“下载”按钮时,将执行以下功能:
$scope.exportFile = function() {
$http.get('http://127.0.0.1:8000/api/excel/download')
.success(function (data, status, headers, config) {
var anchor = angular.element('<a/>');
anchor.attr({
href: 'data:application/octet-stream;charset=utf-8,' + encodeURI(data),
target: '_blank',
download: 'spreadsheet.xls'
})[0].click();
})
.error(function (data, status, headers, config) {
// handle error
});
};
The returned spreadsheet contains unreadable characters. 返回的电子表格包含不可读的字符。
If I access http://127.0.0.1:8000/api/excel/download directly, the spreadsheet is downloaded without the .xls extension (with no extension at all). 如果我直接访问http://127.0.0.1:8000/api/excel/download ,则下载的电子表格不带.xls扩展名(根本没有扩展名)。 If I rename the file adding the .xls extension and then open it, I can see the file contents.
如果重命名添加.xls扩展名的文件,然后打开它,则可以看到文件内容。 So I think the problem it's on the call Angular does to back-end and not on generating the file on Java.
所以我认为问题出在Angular对后端的调用上,而不是在Java上生成文件。
Has anyone experienced this situation or have some example to share? 有没有人遇到过这种情况或可以分享一些例子? What am I doing wrong?
我究竟做错了什么?
The Content-Disposition
header should do the trick. Content-Disposition
标头应该可以解决问题。 So, you would have something like this: 因此,您将获得以下内容:
HttpHeaders headers = new HttpHeaders();
headers.setContentDispositionFormData("Attachment", "spreadsheet.xls");
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
headers.setContentLength(file.length);
return new ResponseEntity<InputStreamResource>(
new InputStreamResource(new ByteArrayInputStream(file)),
headers, HttpStatus.OK);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.