gpt4 book ai didi

javascript - 文件下载后重新加载页面

转载 作者:行者123 更新时间:2023-12-02 09:27:02 25 4
gpt4 key购买 nike

我正在提供 PDF 格式的 jasper 报告以供下载。但是当我这样做时,我无法使页面重新加载或重定向。

导致下载发生的页面使用表单提交来开始文件下载。 Govinda Sakhare 提供的答案是在我做出此澄清之前发布的。然而,他的答案可以通过很少的工作来实现,因为表单只有一种选择(大或小)。

处理服务器响应的函数如下:

    private void generateReportPDF(JasperReport jasperReport, List<? extends Object> data, Map<String, Object> parameters, HttpServletResponse resp) throws Exception {
byte[] bytes = null;
if( data == null ) {
bytes = JasperRunManager.runReportToPdf(jasperReport, parameters, reportDao.getConnection());
}
else {
bytes = JasperRunManager.runReportToPdf(jasperReport, parameters, new JRBeanCollectionDataSource(data));
}
resp.reset();
resp.resetBuffer();
resp.setContentType("application/pdf");
resp.addHeader("Content-Disposition", "attachment; filename=" + jasperReport.getName() + ".pdf");
resp.setContentLength(bytes.length);

ServletOutputStream ouputStream = resp.getOutputStream();

ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();
return;
}

由于此功能,我的 Controller 无法重定向页面,当我尝试重定向时,我收到以下错误。

SEVERE: Servlet.service() for servlet [myapp-dispatcher] in context with path [/myapp] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed] with root cause
java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed

我想重新加载页面。这是在标签打印屏幕上,文件下载后,我希望页面重新加载/重定向到自身,以便打印的标签显示在新位置。由于 Controller 无法重定向,我必须修改处理响应的原始流程或实现 JavaScript 以在处理文件后发生。

这个question询问捕获下载并响应它们,但不涉及 spring,因此答案将需要比我想做的更多更改。

使用 answers 之一从这个问题我设法找到了一个解决方法,但它并不令我满意。

 $(document).ready(function() {
$('#printBtn').click(function() {


if(confirm('<spring:message code="print.alert.confirm"/>') ? true : false)
{
window.addEventListener('focus', window_focus, false);
function window_focus(){
//remove buttons
var elem1 = document.getElementById('printBtn');
elem1.parentNode.removeChild(elem1);
//elem1.parentNode.replaceChild(newbutton);
var elem2 = document.getElementById('clearBtn');
elem2.parentNode.removeChild(elem2);
var elem3 = document.getElementById('restoreBtn');
elem3.parentNode.removeChild(elem3);
document.getElementById('button_spot').innerHTML = "The page will reload after you get the file.";

//watch for page to lose focus due to download dialog box
window.addEventListener('focusout', pageNoFocus);
function pageNoFocus(){
//watch for page to resume focus
window.removeEventListener('focusout', pageNoFocus);
window.addEventListener('focus', pageFocus);
function pageFocus(){
window.removeEventListener('focus', pageFocus);
location.reload();
}
}
}

return true;
}
return false;


});

$('#clearBtn').click(function() {
return confirm('<spring:message code="print.alert.clear"/>') ? true : false;
});

$('#restoreBtn').click(function() {
return confirm('<spring:message code="print.alert.restore"/>') ? true : false;
});
});

最佳答案

您可以使用以下代码片段下载文件并重定向到某个 URL。

<a href="#" id="download">Download</a>
<a href="downloadfileURL" target="_blank" id="downloadFile" />
<!-- change href with Spring mapping which will download the file -->

单击下载链接时,它将单击指向实际的 anchor 网址。
该文件将被下载,然后它将重定向到 window.location.href

$("#download").click(function () {
$("#downloadFile")[0].click();
window.location.href = "/abc.html"; // change to the desired URL
});

如果您通过表单提交下载文件,请使用下面的代码片段。

$("#formId").click(function (e) {
e.preventDefault();
$("#downloadFile")[0].click();
window.location.href = "/abc.html"; // change to the desired URL
});

关于javascript - 文件下载后重新加载页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58271984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com