Angular 5- PDF文档,下载并在新窗口中显示

标签 angular pdf iframe

我正在使用 Angular 作为前端,并且正在从 REST 完整 Web 服务获取数据。 1) 我需要在网络浏览器的新窗口中显示 pdf 文件,我正在从网络服务获取基于 64 的数据。

问题:我能够下载该文件,但当我尝试在新窗口中打开该文件时,它会显示(错误:无法加载 pdf 内容)。这是我的代码:

服务:

this.http.get('https://localhost:44364/api/v1/source/GetImage/parameter1/animal.pdf', { responseType:'arraybuffer' })
    .subscribe(response => {
    var file = new Blob([response], {type: 'application/pdf'});
    var fileURL = URL.createObjectURL(file);

    this.content=this.sanitizer.bypassSecurityTrustResourceUrl(fileURL);
});

HTML:在 HTML 中我使用 IFRAME 来显示 pdf 数据

iframe class="e2e-iframe-trusted-src" width="640" height="390" [src]="content"

有谁可以帮帮我吗?谢谢

最佳答案

要下载 pdf 文件,您可以尝试下面的代码。您无法在桌面上打开 pdf 文件。

  this.httpClient.get('../assets/sample.pdf', { responseType: 'arraybuffer' 
    }).subscribe(response => {
      console.log(response);

      const file = new Blob([response], {type: 'application/pdf'});
      const fileURL = window.URL.createObjectURL(file);
      /*const link = document.createElement('a');
      link.href = fileURL;
      link.download = 'sample.pdf';
      link.click();*/
      if (window.navigator.msSaveOrOpenBlob) {
           window.navigator.msSaveOrOpenBlob(file, fileURL.split(':')[1] + '.pdf');
      } else {
           window.open(fileURL);
      }
   });

}

关于Angular 5- PDF文档,下载并在新窗口中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53843995/

相关文章:

javascript - 是否可以篡改 Angular 应用程序中的客户端代码?

Angular 6/7 "the result of a dependency is an expression"

javascript - Angular - 在事件监听器函数内调用服务函数返回未定义的错误

c# - PDF -Adobe 数字版

javascript - 检测页面中的所有iframe是否都有src

javascript - 如何让同域 iframe 禁用同域脚本

angular - 如果规范位于循环内, Protractor 测试始终会通过

html - 在分页符前的最后一行添加边距/填充

java - 合并两个pdf时如何向输出pdf添加页码?

javascript - 如何根据其内容的高度调整 iframe 的高度?