Download file from JSF with provided answer

I have dynamically generated files that I want the JSF 2.0 application to load for the user. I managed to get this working using the code found in the solution:

Force save as a dialog from any web browser from a JSF application

and a command button in the form on the page

And it works great, with the exception of one hit. I want you to be able to send a message to the user on the start page, which says that their file is being processed and wait for it to wait. Obviously, calling callComplete terminates this. Is there a way to redisplay the send page and send the file back with the same button?

+1
source share
2 answers

No, you can’t. You can send only one response for each request. The best you can do is use JavaScript to display the originally hidden div or something that contains the message during onclick . But you will have a problem that you cannot hide it whenever the download is complete.

An alternative is to save the file to a temporary disk and return a full JSF response in which you show a download link that returns the file from the temporary disk to a stand-alone servlet.

+2
source

I think you can use ajax to solve this problem. Call the method that creates the file from the ajax action and provide a javascript callback to handle navigation or to display a layer or whatever

  <script type="text/javascript"> function processEvent(data) { if (data.status == "begin") { showWaitingLayer(); } else if (data.status == "success") { hideWaitingLayer(); showDownloadLink(); } } </script> <h:commandLink action="#{myBean.createDocument}"> <f:ajax onevent="processEvent"/> </h:commandLink> 
-1
source

Source: https://habr.com/ru/post/1201913/


All Articles