繁体   English   中英

从Angular组件打开PDF Blob

[英]Open PDF blob from Angular Component

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

我首先遵循了这篇文章给出的指导,所以现在我有这两种方法:

在调用组件中

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文件时,我可以看到内容不是二进制的,而是字符串的完整字节数组。

在此处输入图片说明

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM