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

问题描述 投票:1回答:1

我从后端获取文件流。标头保留带扩展名的文件名。但是如何在最后获得这些财产。这是我的代码,没有得到值也没有错误。

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)
        )
    }

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

rxjs angular7 angular-httpclient
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.