A command link is present on the JSF page. When the link is clicked, a modal is opened.
The download button in the modal is selected, validation takes place for the input fields and then a file is downloaded. The requirement is to close the modal and return to the same page after successful download of the file. How can we close the modal when the Open File dialog is showing up on the same page.
<h:panelGroup rendered="#{downloadBacking.showDownloadModal}">
<ui:include src="DownloadFile.xhtml" />
</h:panelGroup>
showDownloadModal field is defined in viewscoped bean.
When download button in modal is clicked, the downloadFile() method is called.
public String downlaodFile(){
// response.getwriter.write(string);
showDownloadModal = false;
return "";
}
Your
downloadFile()method won’t work that way. You can send only one response per request. A file download is at its own already a whole response. You cannot append another data to it; it would only corrupt the downloaded file with that data. Or, if you’ve written the download file code properly, you should also have seen anIllegalStateException: response already committed.You have 2 options:
Close the dialog immediately on click of the download link with help of some JS code.
Save the file server’s temp disk (or as
byte[]in memory if it isn’t large) and return to the same page wherein you hide the dialog and conditionally render some JSwindow.locationwhich points to that file. This is particularly useful if the to be downloaded file actually needs to be created/generated first which may be time consuming.The URL can point to a simple servlet which streams the file from disk or memory.