简体   繁体   English

从Angular组件打开PDF Blob

[英]Open PDF blob from Angular Component

In my Angular application, I need to open a PDF when a button is clicked. 在我的Angular应用程序中,单击按钮时需要打开PDF。

I followed the directions given by this post in the first place, so I have these two methods right now: 我首先遵循了这篇文章给出的指导,所以现在我有这两种方法:

In the calling component 在调用组件中

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

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

In the service 在服务中

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's response contains an array like the following [ 37,80,68,70,45,49,46,...] . GetPdf的响应包含一个类似于以下[ 37,80,68,70,45,49,46,...]的数组。 Unfortunately, when the download method is called, the result seems to be an invalid pdf. 不幸的是,当调用下载方法时,结果似乎是无效的pdf。 When downloading the file via firefox and renaming it to a txt-file, I can see that the content is not binary but the complete byte array as a string. 通过firefox下载文件并将其重命名为txt文件时,我可以看到内容不是二进制的,而是字符串的完整字节数组。

在此处输入图片说明

As indicated in the comments by @David Siro, The Service had to be modified. 如@David Siro的评论所指出,该服务必须进行修改。 The request now returns a stream rather than a byte array, which can be handled as described in the question. 现在,请求返回一个流而不是字节数组,可以按照问题中的描述进行处理。

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

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