Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP response header content disposition for attachments

Tags:

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.

like image 782
Dave Jarvis Avatar asked Mar 11 '11 22:03

Dave Jarvis


People also ask

What is content-disposition attachment filename?

Content-Disposition is an optional header and allows the sender to indicate a default archival disposition; a filename. The optional "filename" parameter provides for this. This header field definition is based almost verbatim on Experimental RFC 1806 by R. Troost and S.

Is content-disposition mandatory?

Content-Disposition is an optional header field.

What is content type and content-disposition?

The Content-Type representation header is used to indicate the resource's original media type prior to any content encoding used for sending. Although the Content-Disposition header is defined in the context of MIME messages for e-mail, only a subset of the possible parameters apply to HTTP forms and POST requests.


2 Answers

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('.')) + "\""); 
like image 119
Muhammad Salman Avatar answered Sep 20 '22 06:09

Muhammad Salman


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); 
like image 23
Buhake Sindi Avatar answered Sep 20 '22 06:09

Buhake Sindi