开发者

Forcing a save as dialogue from any web browser from JSF application [duplicate]

This question already has answers here: 开发者_StackOverflow How to provide a file download from a JSF backing bean? (5 answers) Closed 7 years ago.

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


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.iana.org/assignments/media-types 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.
}


Use the content-disposition: attachment HTTP header


sometimes you need to force the writer to send the contents to the client by calling response.getWriter().flush(); before closing the writer. This prompted the save as popup in my case.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜