我从后端获取文件流。标题保留带有扩展名的文件名。但是如何在前端获取这些属性。这是我的代码,没有得到值也没有错误。
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)
)
}
没有得到回应。请有人帮我获取响应标头吗?
从文档中,您需要添加observe: 'response'
到选项中才能访问完整的响应对象。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句