如何使用 angular HttpClient 获得响应 headers

How to get response headers using angular HttpClient

我在这里使用 rxjs Ajax 进行 post api 调用并从响应 headers 中获取文件名并下载文件。我需要用 angulars 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'));
  });

来自这个答案: