2

我正在使用 ng2-pdfjs-viewer 显示用户已上传到我的服务器的 PDF。每次用户在他们的视图中单击文件时,都会从服务器检索文件并将其制成一个 blob 并提供给 ng2-pdfjs-viewer 以显示在 externalWindow 中。这适用于除 IE11 之外的所有浏览器。

起初它在 IE 上工作正常,但如果我多次关闭并打开同一个文件,过一会儿 pdf 查看器会在检索 PDF 'blob:B04FA5CB-1012-4CBC-8DFB- 时给我一个“意外的服务器响应 (500)” 59C4CE02C34A'"。如果我用 IE 打开网站的第二个实例,也会发生这种情况。当它开始出现此错误时,必须关闭整个浏览器,然后我必须返回该网站并重试 - 然后它再次工作。

我向服务器发出的所有请求都顺利通过,我与 Fiddler 进行了核对。唯一提供给查看器的是 blob,所以我不明白它在哪里得到了意外的服务器响应。一切都可以在其他所有浏览器上完美运行。

如果没有 pdf 查看器,则在所有浏览器上都可以毫无问题地检索文件,并且可以在 IE 上使用例如 msSaveOrOpenBlob 打开它们。pdf查看器似乎引起了麻烦。

<!--x.component.html code-->
<ng2-pdfjs-viewer #pdfViewer style="width: 800px; height: 400px"
  [externalWindow]="true"
  [downloadFileName]="'document.pdf'"
  [openFile]="false"
  [viewBookmark]="false"
  [download]="true">
</ng2-pdfjs-viewer>

//x.component.ts code

@ViewChild('pdfViewer', {static: false}) pdfViewer;

openDocument(doc: Document) {

    this.storageService.fetchDocument(doc).subscribe(
      res => {
        let type: string = res['type'];
        let file: string = res['file'];
        var promise = this.uploadService.decodeFromBase64(file);
        promise.then((result:string)=> {
          var byteArray = [];
          for (let i = 0; i < result.length; i++) {
            byteArray.push(result.charCodeAt(i));
          }
          const blob = new Blob([new Uint8Array(byteArray)], {type: type});
          var fileURL = URL.createObjectURL(blob);
          if (type == "application/pdf") {
              this.pdfViewer.pdfSrc = fileURL; 
              this.pdfViewer.refresh();
          }
          else {
              //...
          }
          })
      }
    }
4

1 回答 1

2

我从另一个 pdfjs-viewer 文档中找到了这一信息,看起来你的 ng2-pdfjs-viewer 也有类似的问题。

快速隐藏和重新显示 PDF(反之亦然)会导致错误。这是因为大部分 pdf.js 都是异步工作的。初始化小部件需要一些时间。如果它在初始化时被破坏,你就会遇到问题。如果它在早期实例仍在被销毁时被初始化,也会发生同样的情况。

将 PDF 放在选项卡中经常会导致此问题。在选项卡之间切换通常意味着其中一个选项卡的内容被隐藏。同时显示新标签的内容。我在使用@angular/material 时观察到了这一点。解决方案是隐藏第一个选项卡并在超时后显示新选项卡

于 2019-10-09T06:43:39.587 回答