angular - Angular 7 从 POST 下载 PDF 文件
问题描述
我正在尝试下载 POST 请求返回的 PDF 文件:
这是我的 Angular 方法:
sendPost: void {
const options = {headers: {'Content-Type': 'application/json'}};
this.http.post('http://localhost:8080/export', JSON.stringify(this.filter),
options )
.subscribe((next) => console.log(next));
}
以及生成 PDF 的 Java 后端:
@CrossOrigin
@RequestMapping(value = "/export/", method = RequestMethod.POST)
public ResponseEntity<byte[]> getFilteredPDF(@RequestBody PdfExportFilter filter) throws IOException, DocumentException {
StructureExporter writer = new FilteredPdfExporter(filter);
Path filePath = Paths.get(writer.getBaseTempDir() + "/filteredExport.pdf");
writer.exportData();
return new ResponseEntity<byte[]>(readPdfBytes(filePath), getHttpHeaders("_filtered_export"), HttpStatus.OK);
}
如何将返回的内容保存byte[]
为 PDF 文件以便下载到客户端?
解决方案
您可以使用从帖子中获得的数据并创建,并按照此解决方案创建可下载的文件
使用浏览器中的 JavaScript 将二进制数据保存为文件
所以数据应该是next
来自console.log(next)
在打字稿中应该是
const a = document.createElement('a');
document.body.appendChild(a);
a.style.display = 'none';
const blob = new Blob(next, {type: "octet/stream"}),
const url = window.URL.createObjectURL(blob);
a.href = url;
a.download = name;
a.click();
window.URL.revokeObjectURL(url);
推荐阅读
- shell - 如果命令在 10 秒内没有输出任何内容,如何退出命令?
- twitter - Ionic 3 的 Twitter Connect 插件中的登录会话失败
- python - 通过 pandas 绘图界面使用颜色条分组散点图
- sql-server - 如何在同一张桌子上获得不同结果的计数
- php - 将动态数组与动态键组合成单个数组
- aws-glue - AWS glueContext 读取不允许 sql 查询
- maven - 未找到 BIRT 运行时
- java - 如何从 solace 中获取所有队列和主题
- c++ - OpenGL - 纹理显示不正确(夹紧?)
- android - Youtube 播放器与 Android 应用程序中的缩略图集成