gpt4 book ai didi

angular - 在 Angular 中使用文件名下载 Excel 文件

转载 作者:搜寻专家 更新时间:2023-10-30 21:18:47 25 4
gpt4 key购买 nike

如何使用 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) 的响应。 excelCreatedResourceorg.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 文件。

最佳答案

根据您的问题,我认为您需要观察完整的响应并从中获取 header 详细信息。在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com