簡體   English   中英

如何使用 angular HttpClient 獲取響應頭

[英]How to get response headers using angular HttpClient

在這里,我使用 rxjs Ajax 發帖 api 調用並從響應標頭中獲取文件名並下載文件。 我需要用角度 http 服務替換 Ajax 調用。 我不知道如何用 http 替換它,因為我得到的 https 響應中沒有 xhr。 需要幫助將此 ajax 呼叫轉換為 http 呼叫。 謝謝:)

ajax({
      url: url,
      method: method,
      responseType: "blob",
      headers: headers,
      body: body,
    }).subscribe((data) => {
      let filename = "";
      let disposition = data.xhr.getResponseHeader("Content-Disposition");
      if (disposition && disposition.indexOf("attachment") !== -1) {
        let filenameRegex =
          /filename\*?=['"]?(?:UTF-\d['"]*)?([^;\r\n"']*)['"]?;?/;
        let matches = filenameRegex.exec(disposition);
        if (matches != null && matches[1]) {
          filename = decodeURIComponent(matches[1]);
        }
      }
      let blob = new Blob([data.response]);
      let link = document.createElement("a");
      link.href = window.URL.createObjectURL(blob);
      link.download = filename;
      link.click();
    });
  }
http
  .get<any>('url', {observe: 'response'})
  .subscribe(resp => {
    console.log(resp.headers.get('X-Token'));
  });

來自這個答案: 從 API 響應中讀取響應標頭 - Angular 5 + TypeScript

暫無
暫無

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

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