开发者

File download from JSF with a rendered response

I have some dynamically generated files which I want my JSF 2.0 app to download for the user. I've been able to get this working using the code found in the solution here :

Forcing a save as dialogue from any web browser from JSF application

and a command button in a form on the page

And that works fine except for one hitc开发者_Python百科h. I'd like to be able to render a message back to the user on the initial page that tells them their file is being processed and to please wait. Obviously the responseComplete call stops that from happening. Is there some way to re-render the submitting page and send back a file from the same button?


No, you can't. You can send only one response back per request. Best what you could do is to use JavaScript to show an initially hidden div or something which contains the message during the onclick. But you'll have the problem that you cannot hide it whenever the download is completed.

An alternative is to store the file on temp disk and return a fullworthy JSF response wherein you display a download link which returns the file from temp disk by a standalone servlet.


I think you can use ajax to solve this. Call the method that creates the file from an ajax action and provide a javascript callback to handle the navigation or to show 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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜