【问题标题】:How to get header properties from backend api using rxjs?如何使用 rxjs 从后端 api 获取标头属性?
【发布时间】:2019-04-06 12:53:02
【问题描述】:

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

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】:

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

【讨论】:

  • @dudadub - 我在有问题的部分添加了我的代码。仍然没有得到任何响应头..你能看看我更新的代码并建议我正确的方法吗?
  • 顺便问一下this.http是什么类型的?
  • 我从后端获取未知对象。我需要在其中获取我的数据。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-26
  • 1970-01-01
  • 2020-07-01
  • 2018-09-18
  • 2018-03-30
  • 2019-09-16
  • 1970-01-01
相关资源
最近更新 更多