【发布时间】:2019-05-01 04:46:07
【问题描述】:
我正在尝试在角度 5 上实现从 db 下载 pdf 的逻辑。我在下载文件中遇到问题。文件已下载,但在打开时抛出错误 - “加载失败”。
我查看了很多博客和问题/答案,但无法在我的代码中找到错误。
In Database -> pdf file content saved as BLOB
Rest 网络服务 -> 在下面的代码中,pdfData 是来自 BLOB 类型列的数据,这里是 java 代码中的 byte[]
@GetMapping("downloadReport/{reportId}")
public StreamingResponseBody downloadDocument(byte[] pdfData) {
return outputStream -> {
//This way working for sample
/*try (InputStream inputStream = new ByteArrayInputStream(
Files.readAllBytes(Paths.get("D:/pdfWithoutPassword.pdf")))) {
IOUtils.copy(inputStream, outputStream);
}*/
//This way not working when data fetched from Database
try (InputStream inputStream = new ByteArrayInputStream(pdfData)) {
IOUtils.copy(inputStream, outputStream);
}
outputStream.close();
};
Angular 5 代码
downloadReport(reportType: string, reportId: string) {
let fileDownloadUrl = environment.apiUrl + "/downloadReport" + "/" + reportId;
return this.httpClient
.get(fileDownloadUrl, { observe: 'response', responseType: "blob"})
.subscribe(response => this.saveFileToSystem(response.body)),
error => console.log("Error downloading the file."),
() => console.info("OK");
}
saveFileToSystem(input: any) {
const blob = new Blob([input], { type: 'application/octet-stream' });
saveAs(blob, "testingpdf1.pdf");
}
寻求帮助以解决此问题。提前致谢!
【问题讨论】:
-
你能分享 stackblitz 以便我们编辑吗?
-
或您从服务器端获得的响应
-
看看this
-
感谢您的所有 cmets。检查一切,并会回复。还将根据 cmets 编辑 que
标签: java angular download blob