![](/img/trans.png)
[英]Angular and Spring rest api communication over https
[英]Files downloaded with Angular 2 over Spring REST API are twice size. Why?
我想从angular 2下载文件。我的问题是,我从Spring REST API获得的所有文件都是文件大小的两倍。
角度片段:
return this.authHttp.get(this.restAPI + '/downloadFile/' + fileId)
.toPromise()
.then(response => {
// var arrayBufferView = new Uint8Array( response.arrayBuffer() );
// var blob = new Blob( [ arrayBufferView ], { type: response.headers.get("Content-Type") } );
// return blob;
return new Blob([response.arrayBuffer()], { type: response.headers.get("Content-Type")})
})
.catch(this.handleError);
春季REST片段:
@RequestMapping(value = "/downloadFile2/{fileId}",
method = RequestMethod.GET)
public void downloadFileHandler2(@RequestHeader("Authorization") String token,
@PathVariable String fileId, HttpServletResponse response) throws IOException {
changeToCustomerDataBase(token);
File file = projectService.readFromDB(fileId);
response.addHeader("Content-Disposition", "attachment; filename="+file.getName());
response.setContentType(Files.probeContentType(file.toPath()));
response.setContentLengthLong(file.length());
FileInputStream fis = new FileInputStream(file);
int c;
while((c = fis.read()) > -1) {
response.getOutputStream().write(c);
}
response.flushBuffer();
fis.close();
}
来自数据库的文件具有原始大小。
怎么了? 我忘记了什么吗? 非常感谢您的帮助。
昨天我遇到了同样的问题并解决了。 希望您或其他任何人都可以使用该解决方案。 将responseType: ResponseContentType.Blob
添加到您使用的authHttp.get的authHttp.get
。
let requestOptions: RequestOptionsArgs = {
...,
responseType: ResponseContentType.Blob
};
.
.
.
return this.http.request(path, requestOptions);
然后使用response.blob()
而不是new Blob([response.arrayBuffer()], { type: response.headers.get("Content-Type")})
对我来说,那很奏效。 希望对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.