从Angular Component打开PDF blob

时间:2017-06-29 12:09:00

标签: angular pdf blob

在我的Angular应用程序中,我需要在单击按钮时打开PDF。

我首先按照this帖子给出的指示,所以我现在有这两种方法:

在通话组件中

public PrepareDownload(){
   this._certificateService
            .GetPdf(args)
            .subscribe(
            result => {
                this.BlobUri = URL.createObjectURL(result);
            }
}

public Download() {
    window.open(this.BlobUri);     
}

在服务中

public GetPdf() : any  {
  var headers = new Headers();
  headers.append("Content-Type", "application/json");
  var payload = {id:215};
  return this._http.post(this.requestUri, 
                         JSON.stringify(payload), 
                         { headers: headers, 
                           responseType: ResponseContentType.Blob })
                   .map((response: Response) => 
                   {
                      return new Blob([response.blob()], { type: 'application/pdf' });
                   });
}

GetPdf的响应包含类似以下[ 37,80,68,70,45,49,46,...]的数组。不幸的是,当调用下载方法时,结果似乎是无效的pdf。当通过firefox下载文件并将其重命名为txt文件时,我可以看到内容不是二进制文件,而是完整的字节数组作为字符串。

enter image description here

1 个答案:

答案 0 :(得分:0)

如@David Siro的评论所示,该服务必须进行修改。 请求现在返回一个流而不是一个字节数组,可以按照问题中的描述进行处理。