title: HTTP response header content disposition for attachments
body: <h2>Background</h2>
  
  <p>Write an XML document to a browser's response stream and cause the browser to display a "Save As" dialog.</p>
  
  <h2>Problem</h2>
  
  <p>Consider the following <code>download()</code> method:</p>
  
  <pre><code>  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();
  </code></pre>
  
  <p>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.</p>
  
  <p>The web page uses the following code for the button:</p>
  
  <pre><code>    &lt;a4j:commandButton action="#{domainContent.download}" value="Create Domain" reRender="error" /&gt;
  </code></pre>
  
  <p>The XML that is generated <em>does not</em> start with <code>&lt;?xml version="1.0"?&gt;</code>, rather the XML content resembles:</p>
  
  <pre><code>&lt;schema xmlns="http://www.jaspersoft.com/2007/SL/XMLSchema" version="1.0"&gt;
    &lt;items&gt;
      &lt;item description="EDT Class Code" descriptionId="" label="EDT Class Code" labelId="" resourceId="as_pay_payrolldeduction.edtclass"/&gt;
    &lt;/items&gt;
    &lt;resources&gt;
      &lt;jdbcTable datasourceId="JNDI" id="as_pay_payrolldeduction" tableName="as_pay.payrolldeduction"&gt;
        &lt;fieldList&gt;
          &lt;field id="payamount" type="java.math.BigDecimal"/&gt;
        &lt;/fieldList&gt;
      &lt;/jdbcTable&gt;
    &lt;/resources&gt;
  &lt;/schema&gt;
  </code></pre>
  
  <h2>Update #1</h2>
  
  <p>Note the following line of code:</p>
  
  <pre><code>response.setHeader( "Content-Disposition", "attachment;filename=" + filename );
  </code></pre>
  
  <h2>Update #2</h2>
  
  <p>Using <code>&lt;a4j:commandButton ... /&gt;</code> is the problem; a regular <code>&lt;h:commandButton .../&gt;</code> performs as expected. Using the <code>&lt;h:commandBUtton .../&gt;</code> prevents the <code>&lt;a4j:outputPanel .../&gt;</code> from refreshing any error messages.</p>
  
  <p>Related <a href="http://seamframework.org/135584.lace" rel="noreferrer">Seam Message</a>.</p>
  
  <h2>Mime Type</h2>
  
  <p>The following mime types do not trigger the "Save As" dialog:</p>
  
  <ul>
  <li><code>"application/octet-stream"</code></li>
  <li><code>"text/xml"</code></li>
  <li><code>"text/plain"</code></li>
  </ul>
  
  <h2>Question</h2>
  
  <p>What changes will cause the <code>a4j:commandButton</code> to trigger a "Save As" dialog box so that the user is prompted to save the XML file (as <code>domain.xml</code>)?</p>
  
  <p>Thank you.</p>