HTTP response header content disposition for attachments

后端 未结 5 1208
醉酒成梦
醉酒成梦 2020-12-24 14:36

Background

Write an XML document to a browser\'s response stream and cause the browser to display a \"Save As\" dialog.

Problem

Consider the foll

相关标签:
5条回答
  • 2020-12-24 15:09

    Problems

    The code has the following issues:

    • An Ajax call (<a4j:commandButton .../>) does not work with attachments.
    • Creating the output content must happen first.
    • Displaying the error messages also cannot use Ajax-based a4j tags.

    Solution

    1. Change <a4j:commandButton .../> to <h:commandButton .../>.
    2. Update the source code:
      1. Change bw.write( getDomainDocument() ); to bw.write( document );.
      2. Add String document = getDomainDocument(); to the first line of the try/catch.
    3. Change the <a4j:outputPanel.../> (not shown) to <h:messages showDetail="false"/>.

    Essentially, remove all the Ajax facilities related to the commandButton. It is still possible to display error messages and leverage the RichFaces UI style.

    References

    • Using a commandButton in a jsf Page to download a file
    • http://www.coderanch.com/t/483892/JSF/java/when-commandButton-commandLink
    • http://seamframework.org/135584.lace
    0 讨论(0)
  • 2020-12-24 15:10

    neither use inline; nor attachment; just use

    response.setContentType("text/xml");
    response.setHeader( "Content-Disposition", "filename=" + filename );
    

    or

    response.setHeader( "Content-Disposition", "filename=\"" + filename + "\"" );
    

    or

    response.setHeader( "Content-Disposition", "filename=\"" + 
      filename.substring(0, filename.lastIndexOf('.')) + "\"");
    
    0 讨论(0)
  • 2020-12-24 15:10

    Try changing your Content Type (media type) to application/x-download and your Content-Disposition to: attachment;filename=" + fileName;

    response.setContentType("application/x-download");
    response.setHeader("Content-disposition", "attachment; filename=" + fileName);
    
    0 讨论(0)
  • 2020-12-24 15:13

    This has nothing to do with the MIME type, but the Content-Disposition header, which should be something like:

    Content-Disposition: attachment; filename=genome.jpeg;
    

    Make sure it is actually correctly passed to the client (not filtered by the server, proxy or something). Also you could try to change the order of writing headers and set them before getting output stream.

    0 讨论(0)
  • 2020-12-24 15:23

    Try the Content-Disposition header

    Content-Disposition: attachment; filename=<file name.ext> 
    
    0 讨论(0)
提交回复
热议问题