从 Angular 中的获取 Http 请求获取 headers

get headers from get Http request in Angular

我正在调用获取 blob 数据的 API。

后端也发送给我 header 中的文件名。

我的实际问题是我无法从 api.

中获取 header

这是我的 service.ts

public openFile(path) {
  let url='/download/';
  let pathFile= new HttpParams().set('pathFile', path);
  return this.httpClient.get(url,{params:pathFile, responseType: 'blob' });

并在 component.ts 中调用服务。当我尝试打印 res.headers 我在控制台中得到 undefined.

openFile(path){
  this.creditPoliciesService.openFile(path).toPromise().then (data => {
    console.log("dataaaaaa",data.headers); // undefined
    var blob = new Blob([data], {type: 'application/pdf'}); 
    if (window.navigator && window.navigator.msSaveOrOpenBlob) {
      window.navigator.msSaveOrOpenBlob(blob);
    }
    else {
      var fileURL = URL.createObjectURL(blob); 
      window.open(fileURL);
    }
  });
}

在开发工具管理中,我获得了响应信息 header,但我无法在响应变量中找到它们。

传递值为“response”的观察键以获得完整的响应

getData() {
 this.http.get(this.url, { observe: 'response' }).subscribe(res => {
   this.headerProperty = res.headers.get('property name here');
 });
}