gpt4 book ai didi

rxjs - 如何使用 rxjs 从后端 api 获取 header 属性?

转载 作者:行者123 更新时间:2023-12-05 06:27:08 24 4
gpt4 key购买 nike

我正在从后端获取文件流。 header 保留文件名和扩展名。但是如何在前端获得这些属性。这是我的代码,没有得到值也没有错误。

downloadFile(id:number):Observable<any> {

const options = { responseType: 'blob' as 'json' }

return this.http.get<any>(environment.baseUrl+`CourseFileUpload/${id}`, options)
.pipe(
map((file) => {
console.log('header', file.headers('Content-Disposition')); //not getting header value...!?
return new Blob([file], {type: "application/octet-stream"})
}),
catchError(this.handleError)
)
}

有人帮帮我吗?

我尝试过这样的建议:

downloadFile(id:number):Observable<any> {

const headers = new HttpHeaders({ observe: 'response'});
const options = { responseType: 'blob' as 'json', headers:headers }

return this.http.get<any>(environment.baseUrl+`CourseFileUpload/${id}`, options )
.pipe(
map(resp => {
if(resp.headers){
const keys = resp.headers.keys();
console.log('file', keys); //nothing consoles!?
}

return new Blob([resp], {type: "application/octet-stream"})
}),
catchError(this.handleError)
)
}

没有回应。请任何人帮助我获得响应标题?

最佳答案

来自documentation您需要将 observe: 'response' 添加到选项中才能访问完整的响应对象。

关于rxjs - 如何使用 rxjs 从后端 api 获取 header 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55535792/

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