1

The problem for me is pretty straight forward. Onclick of an anchor tag , I execute a javascript using a4j:jsFunction and the action of this function should stream an XML file from server. The problem is , the stream sent on richfaces response doesnt give a saveAs dialog but instead renders the xml on the browser. After reading many articles I understood that Ajax response cannot give a saveAs Dialog.

xhtml snippet:

<h:form>
        <a4j:jsFunction name="updateCart" reRender="idFavouritePanel">
            <a4j:actionparam name="jsonObject" assignTo="#{archiveOrderBean.jsonObject}"/>
        </a4j:jsFunction>

         <a4j:jsFunction  name="download" reRender="partTableId" action="#{archiveOrderBean.loadSelectedOrder}">
            <a4j:actionparam name="strId" assignTo="#{archiveOrderBean.strId}"  />
        </a4j:jsFunction>
</h:form>

and the response set from bean.

response.setContentType("application/xml");
                    response.setContentLength(byteArr.length);
                    response.addHeader("Content-Disposition", "attachment; filename=" + attr.getUrl());
//                  writer.write(byteArr.toString());
//                  writer.flush();
                    response.getOutputStream().write(byteArr);
                    response.getOutputStream().flush();
                    // post(trueStr,encPath,encUrl,trueStr,response);
                    FacesContext.getCurrentInstance().responseComplete();

any help in this regard will be really helpful.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
chaosguru
  • 1,933
  • 4
  • 30
  • 44

2 Answers2

3

Yes, you can't trigger saveAs dialog with ajax. What you can do is return a URL. Then open that URL (with javascript). The server should then send the file (with the appropriate headers). Of course, you can skip all that and just give a link to the URL in question (unless it is dynamically generated)

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Thank you. I too was thinking the same way but was waiting if I could get some more learning on this :-) – chaosguru Oct 07 '11 at 16:23
  • 1
    @chaosguru: Ajax requests are handled by JavaScript code. JavaScript **can not** process a binary response and force a *Save As* dialog. You really have to send a fullworthy synchronous request by a normal button or link. The current page will just remain unchanged in the browser. There's no need for JS/Ajax in this. – BalusC Oct 07 '11 at 16:48
  • @BalusC: Thanks alot, Your description was really helpful. I will do the changes accordingly. – chaosguru Oct 10 '11 at 08:40
0

I could solve this problem with the crude method not very recommended but with no alternative.

I added a hidden h:commandButton and moved the attribute action="#{archiveOrderBean.loadSelectedOrder}" from the a4j:jsFunction, On completion of javascript execution I explicitly call the click event for h:commandButton using javascript. This will synchronous request for the XML. In short decieving the click event.

This was one way I had to do the changes since the XML was created at runtime. The other way is as explained by @Bozho.

Thank you @BalusC for your help :-)

chaosguru
  • 1,933
  • 4
  • 30
  • 44