[英]Can't manage to download PDF retrieved from BLOB via Servlet
I looked around and tried a few solutions I saw, but nothing seemed to work. 我环顾四周,尝试了一些看到的解决方案,但似乎没有任何效果。
So here is my case : 所以这是我的情况:
I have a PDF file stored in my oracle database as BLOB. 我在Oracle数据库中以BLOB格式存储了一个PDF文件。 In my backend, I call my service in order to download that pdf (In my entity, the pdf is a
byte[]
) like this : 在我的后端,我打电话给我的服务以便下载该pdf(在我的实体中,pdf是
byte[]
),如下所示:
@GET
@Path("/downloadpdf")
@Produces("application/pdf")
public HttpServletResponse downloadPdf(@Context HttpServletRequest request, @Context HttpServletResponse response) {
try {
UserGuideJpaService userGuideService = new UserGuideJpaServiceImpl();
HashMap<String, Object> result = userGuideService.getPdfGuide();
if ("0".equals(result.get("returnCode"))) {
UserGuide userGuide = (userGuide) result.get("userGuide");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-control", "private");
response.setDateHeader("Expires", 0);
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=\"APP - User Guide.pdf\"");
byte[] pdf = userGuide.getPdf();
if (pdf != null) {
response.setContentLength(pdf.length);
ServletOutputStream out = response.getOutputStream();
out.write(pdf);
out.flush();
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
Back to my front-end, I have this : 回到我的前端,我有这个:
NOTE : This is what my response.data
looks like (it's a string) : 注意:这是我的
response.data
样子(它是一个字符串):
%PDF-1.4 %
%PDF-1.4%
8 0 obj << /Type /Page /Resources << /ProcSet [ /PDF /Text ] /Font 4 0 R /Shading 6 0 R /ExtGState 7 0 R
8 0 obj << / Type / Page / Resources << / ProcSet [/ PDF / Text] / Font 4 0 R /阴影6 0 R / ExtGState 7 0 R
/MediaBox [0 0 595.28 864.00] /Contents 9 0 R /Parent 10 0 R >> endobj
/ MediaBox [0 0 595.28 864.00] / Contents 9 0 R / Parent 10 0 R >> endobj
9 0 obj << /Length 1679 /Filter [ /FlateDecode ] >> stream x X n7 ?] 84 aw =I Jr v %ڝ@ Q a s + r %E_- % {x | 1 o Oo { ߌj bCN_3 81 { ̭] 9 rXU e T]WV m q5|\\5 a Ꞽ" T0n~" O X 9 #~+ b 5 7 9 ) R/f 5xF 5 WW eA lr}A.' 7Ej iƳ ] \\ ^ +f ܦ UT < 1 , ZrE3 s 4 bzU,f 4 ̎ U *7 ̋ vN 4 ' 䌜 ^ Nf#! ~2 \\G+ _ , b< 2 / WIU V 2% B {Z d H ̀R . g%@ 0Ln) 9...
9 0 obj << / Length 1679 / Filter [/ FlateDecode] >>流x X n7 ?] 84 aw =I Jv %ڝ@ Q a。 s + r%E_- % {x | 1 o Oo { j bCN_3。 81 {̭] 9rXUeT] WVmq5 | \\5aꞼ “T开〜” OX 9 #〜+ b 5 7 9 ) R/f 5xF 5 WW eA lr} A。 '7Ej iƳ ] \\ ^^ +f UT < 1 ,ZrE3 s 4 bzU,f 4 ̎ U. *7̋vN4' 䌜^ Nf个#!〜2 \\ G + _,b <2 / WIU V2%B {ZdHR.g%@0Ln)9 ...
And javascript side : 和javascript端:
$scope.downloadPdf = function() {
APIClientService.downloadPdf().then(function(response) {
// #1
var URL, blob, downloadLink, downloadUrl;
downloadLink = document.createElement('a');
downloadLink.target = '_blank';
downloadLink.download = 'APP - User Guide.pdf';
blob = new Blob([response.data], {
type: 'application/pdf'
});
URL = window.URL || window.webkitURL;
downloadUrl = URL.createObjectURL(blob);
downloadLink.href = downloadUrl;
document.body.append(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
URL.revokeObjectURL(downloadUrl);
// # 2
window.open("data:application/pdf," + encodeURI(response.data));
// # 3
window.open("data:application/pdf," + escape(response.data));
});
};
However, this doesn't download the file or even open it. 但是,这不会下载文件,也不会打开文件。
And I'm pretty sure the pdf is correct in the backend because I managed to get it when using : 而且我很确定pdf在后端是正确的,因为我在使用时设法得到了它:
OutputStream out = new FileOutputStream("Test.pdf");
out.write(pdf);
out.close();
How can I then download the pdf form my browser ? 我该如何从浏览器下载pdf表格? Or at least open it ?
还是至少打开它?
Edit : This is the method from APIClientService : 编辑:这是来自APIClientService的方法:
({
downloadPdf: function() {
return $http({
method: 'GET',
url: this.baseUrl + "/downloadpdf"
});
}
});
By default ajax requests treat you response as text, this will cause issues with non text files. 默认情况下,ajax请求会将您的响应视为文本,这将导致非文本文件出现问题。
To prevent this add a binary responseType to your request, something like 为了防止这种情况,请向您的请求中添加一个二进制responseType,例如
({
downloadPdf: function() {
return $http({
method: 'GET',
url: this.baseUrl + "/downloadpdf",
responseType: "blob"
});
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.