从Angular获取Http请求获取头文件 [英] get headers from get Http request in Angular

查看:180
本文介绍了从Angular获取Http请求获取头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm making a call to an API that gets blob data.

后端还向我发送文件名称。

back end sends to me also file name in header.

我的实际问题是我无法从API获取标题。

My actual problem is that I can't get header from the api.

这是我的 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 时,我在控制台中得到了未定义。

and in component.ts I call the service. when I try to print the res.headers I get undefined in console.

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

在开发工具管理员中,我在响应头中获取信息,但我不是能够在响应变量中找到它们。

In the dev tool admin I get informations in response header but I'm not able to find them in the response variable.

推荐答案

传递具有'response'值的observe键以获得完整的响应

pass observe key with value of ‘response’ to get the complete response

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

 });
}

这篇关于从Angular获取Http请求获取头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆