相关疑难解决方法(0)

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

我正在调用一个获取 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)

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

httprequest http-headers typescript angular-services angular

4
推荐指数
1
解决办法
1万
查看次数