angularjs - 无法在 Angular 应用程序中显示 PDF 文件
问题描述
我有一个 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'
,但它也没有成功。
以下是我看过的主题:
解决方案
我不确定你为什么要使用httpClient
. 您想要的结果可以通过以下代码简单地实现
在 *.service.ts 中:
fetchPDF(): any {
const path = 'assets/pdf/fileName.pdf'
return path;
}
在 *.component.ts 中:
openPDF() {
window.open(this.myService.fetchPDF(), '_blank');
}
推荐阅读
- angular - 为什么在许多教程中通过 npm 构建的节点模块包含在 .dockerignore 中,我们不需要它们来运行我们的应用程序
- javascript - 对齐 React Range Slider 的拇指,使其不会超出边界框
- rxjs - 为什么 rxJs api 中存在不对称性
- c# - 将 JSON 中的数组转换为列表
- matlab - BLE模块RN4871无法与Matlab连接
- flutter - 颤动的tflite对象检测在物理设备上不起作用
- django - 更新通过 ForeignKey 连接的模型
- mongodb - MongoDB聚合双重查找和管道
- python - django cache_page 如何设置版本
- c# - Discord.Net 角色不会在机器人中更新