4

我正在调用获取 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' });

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

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

4

1 回答 1

6

传递值为“响应”的观察键以获得完整的响应

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

 });
}
于 2018-05-02T13:02:08.690 回答