我正在使用ng2-pdfjs-viewer来显示用户上传到我的服务器的PDF。用户每次单击视图中的文件时,都会从服务器检索该文件,并将其转换为blob并提供给ng2-pdfjs-viewer以在externalWindow中显示。这适用于除IE11以外的所有浏览器。
起初,它在IE上运行良好,但如果我多次关闭并打开同一个文件,一段时间后,pdf查看器会给我一个“在检索PDF‘blob时出现意外的服务器响应(500):B04FA5CB-1012-4CBC-8DFB-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 {
//...
}
})
}
}发布于 2019-10-09 14:43:40
我从另一个pdfjs-viewer文档中找到了这个信息,看起来你的ng2-pdfjs-viewer也有类似的问题。
快速隐藏和重新显示PDF (反之亦然)会导致错误。这是因为pdf.js的大部分是异步工作的。初始化小部件需要一些时间。如果它在初始化时被销毁,你就会遇到问题。如果在初始化之前的实例仍在销毁时,也会发生同样的情况。
将PDF放在选项卡中经常会导致此问题。在选项卡之间切换通常意味着其中一个选项卡的内容被隐藏。同时,显示新选项卡的内容。我在使用@angular/material时观察到了这一点。解决方案是隐藏第一个选项卡,并在超时后显示新选项卡
https://stackoverflow.com/questions/58292812
复制相似问题