繁体   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