【发布时间】:2020-08-03 18:27:48
【问题描述】:
我有一个 PDF 文件存储在应用程序 (assets/pdf/fileName.pdf) 的目录中。我需要在对话框中单击按钮时将其显示在新选项卡上。
在查看了各种答案后,这是我所拥有的:
在 *.component.ts 中:
openPDF() {
this.myService.fetchPDF().subscribe(
res => {
let file = new window.Blob([res], {type: 'application/pdf'});
let fileURL = window.URL.createObjectURL(file);
window.open(fileURL, '_blank');
}
);
}
在 *.service.ts 中:
fetchPDF(): any {
const path = 'assets/pdf/fileName.pdf';
return this.httpClient.get(PathResolver.resolveStatic(path),{responseType : 'blob'});
}
我已经尝试过使用responseType : 'arraybuffer',但也没有成功。
这是我看过的主题:
【问题讨论】:
-
你当前代码的结果是什么?
-
@amanpurohit:在 Chrome 上,新标签页会立即打开和关闭。在 Firefox 上,我能够看到错误,说明 PDF 文件行中的某些内容已损坏或无效。