我環顧四周并嘗試了一些我看到的解決方案,但似乎沒有任何效果。所以這是我的情況:我有一個 PDF 文件作為 BLOB 存儲在我的 oracle 數據庫中。在我的后端,我調用我的服務以下載該 pdf(在我的實體中,pdf 是 a 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;}回到我的前端,我有這個:注意:這就是我的response.data樣子(它是一個字符串):%PDF-1.4 %8 0 obj << /Type /Page /Resources << /ProcSet [ /PDF /Text ] /Font 4 0 R /Shading 6 0 R /ExtGState 7 0 R/MediaBox [0 0 595.28 864.00] /Contents 9 0 R /Parent 10 0 R >> endobj但是,這不會下載文件,甚至不會打開它。而且我很確定 pdf 在后端是正確的,因為我在使用時設法得到它: OutputStream out = new FileOutputStream("Test.pdf"); out.write(pdf); out.close();我怎樣才能從我的瀏覽器下載 pdf 格式?或者至少打開它?
1 回答

飲歌長嘯
TA貢獻1951條經驗 獲得超3個贊
默認情況下,ajax 請求將您的響應視為文本,這會導致非文本文件出現問題。
為了防止這種情況在您的請求中添加一個二進制 responseType,例如
({
downloadPdf: function() {
return $http({
method: 'GET',
url: this.baseUrl + "/downloadpdf",
responseType: "blob"
});
}
});
添加回答
舉報
0/150
提交
取消