1
嘗試將PDF從一個servlet發送到客戶端。客戶端通過AJAX發送POST請求,並由servlet處理該請求以生成PDF並將PDF作爲響應發送給客戶端。從servlet開始提供PDF
我試圖(讓空/未打開的PDF文件)張貼在這裏沒有用的選項:Opening PDF String in new window with javascript
任何幫助將apprecieated!
的Java Servlet:
response.setContentType("application/pdf");
response.setHeader("Content-disposition","attachment; filename=ProgressReport.pdf");
response.setContentLength((int) pdfFile.length());
OutputStream out = response.getOutputStream();
FileInputStream in = new FileInputStream(pdfFile);
byte[] buffer = new byte[1024];
int length =0;
while ((length = in.read(buffer)) != -1){
out.write(buffer, 0, length);
System.out.println(buffer);
}
in.close();
out.flush();
JS
$.ajax({
url : "GenerateReport",
data : {
...
},
type : "POST",
success : function(result) {
console.log(result);
//window.open("data:application/pdf, " + encodeURI(result));
//download(result);
},
error : function(result) {
...
}
})
到目前爲止,我可以使用此代碼只得到在瀏覽器控制檯格式化的PDF字符串印刷瀏覽器控制檯中的PDF字符串
%PDF-1.4 %����3 0 obj<</Filter/FlateDecode/Length 238>>streamx��QMO�@��x��(��D��!A�x�R��T�-�n��{�5LDB�e2�y�>2�l�Y$1�:a�i.�"�~f ...
嗨,我已經編輯我的servlet代碼的文件爲Base64編碼,並使用'「數據:應用程序/ PDF格式;的base64」 +結果;' 然而,這僅僅是下載一個空白的PDF 。你知道這可能是爲什麼嗎? –
如果我首先使用'window.atob(result)解碼結果並使用''data:application/pdf;「+ result;」傳遞它,它也會打開一個空白PDF –
對不起,我不確定什麼如果你解碼這個主機上的base64,並且保存文件是正確的大小,會發生什麼? –