angular - Angular 5- PDF 文档,下载并在新窗口中显示
问题描述
我正在使用 Angular 作为前端,并且从 REST Full Web Service 获取数据。1)我需要在 Web 浏览器的新窗口中显示 pdf 文件,我从 Web 服务获取数据作为 base 64。
问题:我可以下载文件,但是当我尝试在新窗口中打开文件时,它会显示(错误:无法加载 pdf 内容)。这是我的代码:
服务:
this.http.get('https://localhost:44364/api/v1/source/GetImage/parameter1/animal.pdf', { responseType:'arraybuffer' })
.subscribe(response => {
var file = new Blob([response], {type: 'application/pdf'});
var fileURL = URL.createObjectURL(file);
this.content=this.sanitizer.bypassSecurityTrustResourceUrl(fileURL);
});
HTML:在 HTML 中,我使用 IFRAME 来显示 pdf 数据
iframe class="e2e-iframe-trusted-src" width="640" height="390" [src]="content"
任何人都可以帮助我。谢谢
解决方案
为了下载 pdf 文件,您可以尝试以下代码。您无法在桌面上打开 pdf 文件。
this.httpClient.get('../assets/sample.pdf', { responseType: 'arraybuffer'
}).subscribe(response => {
console.log(response);
const file = new Blob([response], {type: 'application/pdf'});
const fileURL = window.URL.createObjectURL(file);
/*const link = document.createElement('a');
link.href = fileURL;
link.download = 'sample.pdf';
link.click();*/
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(file, fileURL.split(':')[1] + '.pdf');
} else {
window.open(fileURL);
}
});
}
推荐阅读
- javascript - 如何截取 div 并将值插入模型的属性 ASP .NET Core
- mysql - 使用 Excel 单元格中的值作为 SQL 查询中 WHERE 子句的条件
- java - 如何解决 Maven Invoker API 警告:Maven 将以交互模式执行,但没有配置输入流
- javascript - 如何检查背景是渐变色还是纯色
- javascript - 如何在 javascript 的这个循环中添加文本?
- angular - Spring Boot Angular Web 应用程序
- xbim - 从 ifcSpace 获取 IfcPolyline
- c++ - 在命名空间 'cv' 中打开 cv 'SparsePyrLKOpticalFlow'
- python - 仅使用 python 置换最后一列
- c - c:根据 SEQ 字段重新排序 TCP 数据包