我有一个现有的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的解决方案。 –