Angularjs 角度:can';下载服务器端生成的电子表格
我正在尝试下载服务器生成的电子表格 我的应用程序前端使用Angular,后端使用Java 以下是后端上接收生成和返回文件请求的方法:Angularjs 角度:can';下载服务器端生成的电子表格,angularjs,rest,spring-web,Angularjs,Rest,Spring Web,我正在尝试下载服务器生成的电子表格 我的应用程序前端使用Angular,后端使用Java 以下是后端上接收生成和返回文件请求的方法: @RequestMapping(value = "download", method = RequestMethod.GET, produces = "application/xls") public ResponseEntity<InputStreamResource> download() throws IOException { Byte
@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)));
}
@RequestMapping(value=“download”,method=RequestMethod.GET,products=“application/xls”)
public ResponseEntity download()引发IOException{
ByteArrayOutputStream文件输出=新建ByteArrayOutputStream();
HSSFWorkbook=新的HSSFWorkbook();
HSSFSheet工作表=工作簿.createSheet(“POI工作表”);
HSSFRow row1=工作表.createRow((短)0);
HSSFCell cellA1=row1.createCell((短)0);
cellA1.setCellValue(“你好!”);
HSSFCellStyle cellStyle=工作簿.createCellStyle();
cellStyle.setFillForegroundColor(HSSFColor.GOLD.index);
cellStyle.setFillPattern(HSSFCellStyle.SOLID\u前景);
cellA1.setCellStyle(cellStyle);
工作簿。写入(归档);
fileOut.close();
byte[]file=fileOut.toByteArray();
返回响应性
.ok()
.contentLength(file.length)
.contentType(MediaType.parseMediaType(“应用程序/八位字节流”))
.body(新的InputStreamResource(新的ByteArrayInputStream(文件));
}
在前端,当用户点击下载按钮时,执行以下功能:
$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
});
};
$scope.exportFile=function(){
$http.get('http://127.0.0.1:8000/api/excel/download')
.success(函数(数据、状态、标题、配置){
var anchor=angular.element('直接下载电子表格时没有.xls扩展名(根本没有扩展名)。如果我重命名文件并添加.xls扩展名,然后打开它,我可以看到文件内容。因此,我认为angular在调用后端时遇到的问题,而不是在Java上生成文件
有没有人经历过这种情况,或者有什么例子可以分享?我做错了什么?标题应该可以做到这一点。因此,您可能会遇到如下情况:
HttpHeaders=newhttpheaders();
headers.setContentDispositionFormData(“附件”、“电子表格.xls”);
headers.setContentType(MediaType.APPLICATION\u OCTET\u STREAM);
headers.setContentLength(file.length);
返回新响应(
新建InputStreamResource(新建ByteArrayInputStream(文件)),
标题,HttpStatus.OK);
mime类型错误。需要发送字符串,然后作为Excel下载。请让我知道我的答案是否解决了您的问题。抱歉,它没有解决。我所做的能够下载文件的操作不是通过使用$http发出GET请求,而是通过一个简单的链接,