[java] HTTP response header content disposition for attachments

Background

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

Problem

Consider the following download() method:

  HttpServletResponse response = getResponse();

  BufferedWriter bw = new BufferedWriter( new OutputStreamWriter(
      response.getOutputStream() ) );

  String filename = "domain.xml";
  String mimeType = new MimetypesFileTypeMap().getContentType( filename );

  // Prints "application/octet-stream"
  System.out.println( "mimeType: " + mimeType );

  // response.setContentType( "text/xml;charset=UTF-8" );
  response.setContentType( mimeType );
  response.setHeader( "Content-Disposition", "attachment;filename="
      + filename );

  bw.write( getDomainDocument() );
  bw.flush();
  bw.close();

In Firefox, the XML content is displayed in the browser window. In IE 7, the XML content is not displayed -- you have to view the document source. Neither situation is the desired result.

The web page uses the following code for the button:

    <a4j:commandButton action="#{domainContent.download}" value="Create Domain" reRender="error" />

The XML that is generated does not start with <?xml version="1.0"?>, rather the XML content resembles:

<schema xmlns="http://www.jaspersoft.com/2007/SL/XMLSchema" version="1.0">
  <items>
    <item description="EDT Class Code" descriptionId="" label="EDT Class Code" labelId="" resourceId="as_pay_payrolldeduction.edtclass"/>
  </items>
  <resources>
    <jdbcTable datasourceId="JNDI" id="as_pay_payrolldeduction" tableName="as_pay.payrolldeduction">
      <fieldList>
        <field id="payamount" type="java.math.BigDecimal"/>
      </fieldList>
    </jdbcTable>
  </resources>
</schema>

Update #1

Note the following line of code:

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

Update #2

Using <a4j:commandButton ... /> is the problem; a regular <h:commandButton .../> performs as expected. Using the <h:commandBUtton .../> prevents the <a4j:outputPanel .../> from refreshing any error messages.

Related Seam Message.

Mime Type

The following mime types do not trigger the "Save As" dialog:

  • "application/octet-stream"
  • "text/xml"
  • "text/plain"

Question

What changes will cause the a4j:commandButton to trigger a "Save As" dialog box so that the user is prompted to save the XML file (as domain.xml)?

Thank you.

This question is related to java ajax jboss richfaces attachment

The answer is


Try the Content-Disposition header

Content-Disposition: attachment; filename=<file name.ext> 

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.


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);

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('.')) + "\"");

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to ajax

Getting all files in directory with ajax Cross-Origin Read Blocking (CORB) Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource Fetch API request timeout? How do I post form data with fetch api? Ajax LARAVEL 419 POST error Laravel 5.5 ajax call 419 (unknown status) How to allow CORS in react.js? Angular 2: How to access an HTTP response body? How to post a file from a form with Axios

Examples related to jboss

How to enable TLS 1.2 in Java 7 Jboss server error : Failed to start service jboss.deployment.unit."jbpm-console.war" SQLException: No suitable Driver Found for jdbc:oracle:thin:@//localhost:1521/orcl PSQLException: current transaction is aborted, commands ignored until end of transaction block JBoss AS 7: How to clean up tmp? How to deploy a war file in JBoss AS 7? How to increase heap size for jBoss server JBoss default password HTTP response header content disposition for attachments PersistenceContext EntityManager injection NullPointerException

Examples related to richfaces

HTTP response header content disposition for attachments

Examples related to attachment

Save attachments to a folder and rename them Download file inside WebView HTTP response header content disposition for attachments Sending email with attachments from C#, attachments arrive as Part 1.2 in Thunderbird Lotus Notes email as an attachment to another email