Javascript 调用jasper PDF并向客户显示,或者能够下载它?
我使用DOJO+SpringMVC,调用ExportPDF.html生成我的pdf 但是由于我脚本中的xhr函数,我在控制台中有了如下@x13&%的外壳,我想这是我的pdf。如何在新网页中显示或允许客户端下载 这是我的exporPDF.html控制器Javascript 调用jasper PDF并向客户显示,或者能够下载它?,javascript,spring-mvc,pdf,dojo,jasper-reports,Javascript,Spring Mvc,Pdf,Dojo,Jasper Reports,我使用DOJO+SpringMVC,调用ExportPDF.html生成我的pdf 但是由于我脚本中的xhr函数,我在控制台中有了如下@x13&%的外壳,我想这是我的pdf。如何在新网页中显示或允许客户端下载 这是我的exporPDF.html控制器 @RequestMapping(value = "/exportarPDF", method = { RequestMethod.GET, RequestMethod.POST }) private @ResponseBody vo
@RequestMapping(value = "/exportarPDF", method = {
RequestMethod.GET, RequestMethod.POST })
private @ResponseBody
void exportarPDF(@ModelAttribute("someBean") somebean someBean,
BindingResult result,HttpServletRequest request,HttpServletResponse response) throws Exception {
response.setContentType("application/pdf");
JasperReport mainReporte = (JasperReport) JRLoader.loadObject(mainJasper);
JasperPrint mainPrint = JasperFillManager.fillReport(mainReporte, parameters, new JRBeanCollectionDataSource(SOMELIST));
ServletOutputStream output = response.getOutputStream();
JasperExportManager.exportReportToPdfStream(mainPrint, output);
output.close();
}
我的pdf生成伟大的,但我可以看到在一个页面或下载它,请帮助我
这是我的脚本方法
xhr("exportarPDF.html", {
query : {
someValue: This Value is received well in the Spring Controller
},
handleAs : "json",
method : "post"
}).then(function(data) {
//I thinks this data is my pdf!
}, function(err) {
alert("Error Interno");
}, function(evt) {
// Handle a progress event from the request if the
// browser supports XHR2
});
这是我对firebug的调试
我也遇到过这种情况。 如中所述 如果您查询的URL实际返回PDF数据,则无法通过xhr执行此操作。 有关说明,请参见链接。 我所做的是在服务器上保存生成pdf并将其名称放入cookie中。 当收到响应时,我通过检查响应头'Content-Type'='application/pdf'来验证它是否为pdf。 然后我调用了一个不同的url,该url使用window.open呈现pdf。使用第二个参数“\u self”,这样就不会打开新的选项卡
if (isPDF(response))
{
var url=$cookies['fileurl'].substring(1,$cookies['fileurl'].length-1);
$window.open('/ws/print?action='+url,'_self');
delete $cookies['fileurl'];
return;
}
您正试图通过AJAX下载文件,但需要的是开始定期下载 假设您的下载链接是exportarPDF.html,则以下内容将完成此工作: HTML: 说明: 当您设置iframe的src时,浏览器的反应就像您要单击链接一样。因此,如果src指向的内容具有标识为二进制文件的内容类型,则会启动标准下载对话框,就像只需单击下载链接一样
<iframe id="invisible" style="display:none;"></iframe>
function downloadPDF() {
var iframe = document.getElementById('invisible');
iframe.src = "exportarPDF.html";
}