我正在使用 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 {
//...
}
})
}
}