我有一個現有的servlet,給了我一個PDF字節數組:生成一個包含Servlet的PDF文件,並顯示PDF在新瀏覽器標籤 - PDF文件是空
byte[] pdf = myService.getPdf(myArgs);
response.setContentType("application/pdf");
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, postcheck=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setContentLength(pdf.length);
ServletOutputStream out = response.getOutputStream();
out.write(pdf);
out.flush();
out.close();
// for test: byte[] pdf
File testfile = new File("C:\\mytestpath\\mytest.pdf");
FileOutputStream fos = new FileOutputStream(testfile);
fos.write(pdf);
fos.flush();
fos.close();
我在我的servlet一個寫測試pdf文件以檢查myService.getPdf()的返回值。測試文件沒問題,我可以在acrobat reader中打開這個有效的文件。
現在我的jQuery的JavaScript代碼:
function generatePDF(url) {
currentRequest = $.ajax({
url: url,
type: "GET",
success: function(data) {
var blob = new Blob([data], {type: 'application/pdf'});
var fileURL = window.URL.createObjectURL(blob);
window.open(fileURL, "_blank");
},
error: function() {
console.error("test in generatePDF error");
// use here other jquery mobile functions
}
});
}
我想生成我的servlet一個PDF字節數組,並顯示在新的瀏覽器選項卡中的PDF。 在我的實現中,打開了一個新的瀏覽器選項卡(地址欄:blob:http%3A // localhost%3A8080/3fd5808b-758b-4076-94c9-af9884f631a3)。但PDF文件是空的,PDF文件的頁面大小是可以的。 我該如何解決這個問題?我需要一個名爲myid.pdf的有效PDF在新的瀏覽器標籤中。
感謝對尤爾提示,托馬斯
我完全同意,也沒有必要使用AJAX或jQuery的請求。您只需瀏覽標題,向瀏覽器建議如何處理文件... +1 – MaVRoSCy
是的,我選擇了一個沒有AJAX的解決方案。 –