簡體   English   中英

從Angular中的獲取Http請求獲取標頭

[英]get headers from get Http request in Angular

我正在調用獲取Blob數據的API。

后端也向我發送標頭中的文件名。

我的實際問題是我無法從api獲取標頭。

這是我的服務

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在控制台中未定義。

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

在開發工具管理員中,我在響應標頭中獲取信息,但無法在響應變量中找到它們。

傳遞觀察值為“ response”的鍵以獲得完整的響應

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

 });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM