[英]Files downloaded with Angular 2 over Spring REST API are twice size. Why?
I want to download files from angular 2. My problem is, that all files I get from the Spring REST API are twice file size. 我想从angular 2下载文件。我的问题是,我从Spring REST API获得的所有文件都是文件大小的两倍。
Angular Snippet: 角度片段:
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);
Spring REST Snippet: 春季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();
}
Files, thar coming from the database, have the original size. 来自数据库的文件具有原始大小。
What is wrong? 怎么了? Did I forget something? 我忘记了什么吗? I'm very grateful for any help. 非常感谢您的帮助。
I had the same Problem yesterday and solved it. 昨天我遇到了同样的问题并解决了。 I hope you or anyone else can use the solution. 希望您或其他任何人都可以使用该解决方案。 Add responseType: ResponseContentType.Blob
to your requestOptions of the authHttp.get
you use. 将responseType: ResponseContentType.Blob
添加到您使用的authHttp.get的authHttp.get
。
let requestOptions: RequestOptionsArgs = {
...,
responseType: ResponseContentType.Blob
};
.
.
.
return this.http.request(path, requestOptions);
Then use response.blob()
instead of new Blob([response.arrayBuffer()], { type: response.headers.get("Content-Type")})
然后使用response.blob()
而不是new Blob([response.arrayBuffer()], { type: response.headers.get("Content-Type")})
For me that worked. 对我来说,那很奏效。 I hope it helps you too. 希望对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.