从 Angular 中的 get Http 请求中获取标头

inf*_*dev 4 httprequest http-headers typescript angular-services angular

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

后端也向我发送标题中的文件名。

我的实际问题是我无法从 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' });
Run Code Online (Sandbox Code Playgroud)

component.ts 中,我调用了该服务。当我尝试打印时,res.headers我在控制台中未定义。

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);
    }
  });
}
Run Code Online (Sandbox Code Playgroud)

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

Fat*_*med 5

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

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

 });
}
Run Code Online (Sandbox Code Playgroud)

  • 我在我的请求中添加了“{observe:'response'}”,但我没有获得在浏览器开发工具中看到的所有标头响应项 (2认同)