如何使用 rxjs 从后端 api 获取 header 属性?
How to get header properties from backend api using rxjs?
我正在从后端获取文件流。 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)
)
}
没有得到回应。请任何人帮助我得到回应 header?
从 documentation 您需要将 observe: 'response'
添加到选项中才能访问完整的响应对象。
我正在从后端获取文件流。 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)
)
}
没有得到回应。请任何人帮助我得到回应 header?
从 documentation 您需要将 observe: 'response'
添加到选项中才能访问完整的响应对象。