首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将blob (来自服务器的响应)插入到ngx-extended-pdf-viewer中?

如何将blob (来自服务器的响应)插入到ngx-extended-pdf-viewer中?
EN

Stack Overflow用户
提问于 2018-12-21 23:21:26
回答 1查看 4.7K关注 0票数 0

我正在设置一个包含pdf查看器的新视图。我没有任何到pdf文件的路径,只是后端返回给我一个包含pdf的blob对象,我必须显示这个pdf。现在我使用的是ngx-extended-pdf-viewer库(链接:https://www.npmjs.com/package/ngx-extended-pdf-viewer)

现在我必须使用来自服务器的响应(返回BLOB)并显示文件。我尝试将response赋予变量,然后在HTML中通过异步管道使用它。下一次尝试是添加新的FileReader对象,在其中传递blob并将其放入src属性中。在这两种情况下,浏览器不会显示pdf并直接下载它。

我的所有尝试:https://ghostbin.com/paste/57akz

编辑:https://ghostbin.com/paste/rb8ou

我只想在src中使用我的blob对象,在不下载pdf文件的情况下正确显示它,等等。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-05 00:13:58

虽然我还没有使用过前述的库,但我在使用blob对象和ng2-pdfjs-viewer(https://www.npmjs.com/package/ng2-pdfjs-viewer)构建pdf密集型门户网站方面取得了相当大的成功。

blob对象的用例如下

代码语言:javascript
复制
<!-- your.component.html -->
<button (click)="openPdf();">Open Pdf</button>
代码语言:javascript
复制
<!-- your.component.ts-->
export class SampelComponent implements OnInit {
  @ViewChild('pdfViewer') pdfViewer
  ...

  private downloadFile(url: string): any {
    return this.http.get(url, { responseType: ResponseContentType.Blob }).map(
      (res) => {
        return new Blob([res.blob()], { type: "application/pdf" });
      });
  }

  public openPdf() {
    let url = "url to fetch pdf as byte array"; (Blob works here)
    this.downloadFile(url).subscribe(
     (res) => {
        this.pdfViewer.pdfSrc = res; // pdfSrc can be Blob or Uint8Array
        this.pdfViewer.refresh(); // Ask pdf viewer to load/reresh pdf
      }
    );
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53887169

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档