如何下载 Angular
应用程序中的服务返回的 Excel 文件?我正在使用file-saver
库。这是我的代码:
let blob = new Blob([response], {type: 'application/vnd.openxmlformat-officedocument.spreadsheetml.sheet;'});
saveAs(blob, 'hello.xlsx');
这是 Angular 服务的响应:
return this.http.get(`${this.reportsUrl}`, { responseType: 'blob'});
我的后端服务(Spring MVC)的响应。 excelCreatedResource
的类型为 org.springframework.core.io.Resource
:
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
.header(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"" + excelCreateResource.getFilename() + "\"")
.body(excelCreateResource);
我不想提供hello.xlsx
。相反,我只想下载文件而不更改服务返回的文件名。如果我直接从浏览器访问该 URL,我就可以下载 Excel 文件。
最佳答案
根据您的问题,我认为您需要观察完整的响应并从中获取标题详细信息。在 Angular 中,你可以使用
来做到这一点let observable$ = this.http.get(`${this.reportsUrl}`, { observe: 'response'});
observable$.subscribe(response => {
let blobObj = response.body; // this should be your blob object (body of response)
let headers = response.Headers //This is a disctionary/HashMap were you'll find the content-desposition header
//.....
})
关于angular - 在 Angular 中下载具有原样文件名的 Excel 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54626162/