tags:

views:

140

answers:

2

Hello

I've created a JSF application, and I want to embed a link in a page which when clicked causes the backing bean to marshall out some xml and force the opening of a save-as download dialogue box so the user can choose a location to save the file. I've already written the JAXB code.

How is this done?

Thanks

+1  A: 

Use the content-disposition: attachment HTTP header

Sjoerd
+3  A: 

Set the HTTP Content-Disposition header to attachment. This will pop a Save As dialogue. You can do that using HttpServletResponse#setHeader(). You can obtain the HTTP servlet response from under the JSF hoods by ExternalContext#getResponse().

In JSF context, you only need to ensure that you call FacesContext#responseComplete() afterwards to avoid IllegalStateExceptions flying around.

Kickoff example:

public void download() throws IOException {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();

    response.reset(); // Some JSF component library or some Filter might have set some headers in the buffer beforehand. We want to get rid of them, else it may collide.
    response.setContentType("application/xml"); // Check http://www.w3schools.com/media/media_mimeref.asp for all types. Use if necessary ServletContext#getMimeType() for auto-detection based on filename.
    response.setHeader("Content-disposition", "attachment; filename=\"name.xml\""); // The Save As popup magic is done here. You can give it any filename you want, this only won't work in MSIE, it will use current request URL as filename instead.

    BufferedInputStream input = null;
    BufferedOutputStream output = null;

    try {
        input = new BufferedInputStream(getYourXmlAsInputStream());
        output = new BufferedOutputStream(response.getOutputStream());

        byte[] buffer = new byte[10240];
        for (int length; (length = input.read(buffer)) > 0;) {
            output.write(buffer, 0, length);
        }
    } finally {
        close(output);
        close(input);
    }

    facesContext.responseComplete(); // Important! Else JSF will attempt to render the response which obviously will fail since it's already written with a file and closed.
}
BalusC
@BalusC - thanks very much indeed. I put the code in and now, when I click the link, the (FF) browser replaces the current page with a page containing the file contents rather than popping up a download window. What might I be doing wrong?
Mark Lewis
Try in another browsers (IE, Chrome) as well or restart FF with a clean profile. It may happen that a webbrowser is configured to be the default application for XML files and that XML files should be opened automatically when downloaded.
BalusC
Oh, also ensure that this isn't an asynchronous (ajaxical) request, but just a synchronous ("plain vanilla") request. I.e. just use `h:commandLink` or `h:commandButton`, but not RichFaces, Ajax4jsf, IceFaces, etc ajaxical-powered `UICommand` components.
BalusC
@BalusC, I've specified save for xml file types in FF, and switched to `h:commandButton` and when I click the button now, the whole page reloads and no save dialog appears. The app will not be supported in IE or Chrome (can't even test with them). Not sure where to look now.
Mark Lewis
Is it inside a `h:form`? Is the method actually invoked? Walk through [this list](http://stackoverflow.com/questions/2118656/hcommandlink-is-not-being-invoked/2120183#2120183) to check one and other.
BalusC