Refresh/navigate Current Page While Opening/downloading File In New Tab/window
Solution 1:
You're basically trying to send 2 responses back to 1 request. This is not ever going to work in HTTP. If you want to send 2 responses back, you've got to let the client fire 2 requests somehow. You were already looking in the right direction for the solution, with little help of JavaScript it's possible to fire multiple requests on a single event (click). Your attempt in onclick
is however not valid, the change of window.location
on click of the submit button, right before the form submit, completely aborts the original action of the button, submitting the form.
Your best bet is to directly navigate to the result page which in turn invokes JavaScript window.open()
on page load, pointing to the URL of the PDF file which you'd like to open. It's namely not possible to send some HTML/JS code along with the PDF file instructing a navigation (as that would obviously corrupt the PDF file). This also means, that you can't return the PDF directly to the form submit request. The code has to be redesigned in such way that the PDF can be retrieved by a subsequent GET request. The best way is to use a simple servlet. You could store the generated PDF temporarily on disk or in session, associated with an unique key, and pass that unique key as request pathinfo or parameter to the servlet in window.open()
URL.
Here's a kickoff example:
Initial form:
<h:form>
...
<p:commandButton...action="#{bean.submit}" /></h:form>
Bean:
public String submit() {
Filefile= File.createTempFile("zertifikat", ".pdf", "/path/to/pdfs");
this.filename = file.getName();
// Write content to it.return"targetview";
}
Target view:
<h:outputScriptrendered="#{not empty bean.filename}">
window.open('#{request.contextPath}/pdfservlet/#{bean.filename}');
</h:outputScript>
PDF servlet (nullchecks etc omitted for brevity; Java 7 assumed for Files#copy()
):
@WebServlet("/pdfservlet/*")publicclassPdfServletextendsHttpServlet {
protectedvoiddoGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
Filefile=newFile("/path/to/pdfs", request.getPathInfo().substring(1));
response.setHeader("Content-Type", "application/pdf");
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-Disposition", "inline; filename=\"zertifikat.pdf\"");
Files.copy(file.toPath(), response.getOutputStream());
}
}
Solution 2:
As BalusC said, Refresh/navigate current page
and opening downloading file
are two different responses, there must be two resquests. I encountered a similar problem. I solved it with jsf ajax successfully.
Here's part of my code:
XHTML:
<h:commandButtonid="download-button"class="download-button"value="download"><f:ajaxevent="click"execute="@form"render=":msg-area"listener="#{myController.checkForDownload}"onevent="checkCallBack" /></h:commandButton><h:commandButtonid="download-button2"class="download-button2"value="download"style="display: none;"action="#{myController.download}"></h:commandButton>
Javascript:
functioncheckCallBack(data) {
var ajaxStatus = data.status;
switch (ajaxStatus) {
case"begin":
break;
case"complete":
break;
case"success":
document.getElementById('download-form:download-button2').click();
break;
}
}
download-button
renders a message area on page and download-button2
triggers a download method. they are two different requests. When the first request completed, the second request will be triggered.
Post a Comment for "Refresh/navigate Current Page While Opening/downloading File In New Tab/window"