Here is an example to download files to a local directory with Cocoon 2.1 Final release. To be more specific, I'll show you how to pop up a downloading box instead of displaying a document into the browser.

I've tried two different methods. There is no need to configure Cocoon particularly. All is made in the sitemap.

  • Using a Reader.
<map:pipeline>
  <map:match pattern="save/*">
    <map:act type="header">
      <map:parameter name="Cache-Control" value="Pragma:no-cache" />
      <map:parameter name="Content-Disposition" value="attachment ; filename={1}.xml" />
    </map:act>
    <map:read type="resource" mime-type="binary/octet-stream" src="{1}.xml">
       <map:parameter name="format" value="xml"/>
    </map:read>
  </map:match>
</map:pipeline>

I this pipeline, you don't have to create custom sitemap components.

*First you have to modify the header to set the Response object. To do that, use the HttpHeaderAction (don't forget to declare it). Let's see the different options :

"Cache-Control" is used to tell not to use the cache.

"Content-disposition" is used to define the document as an attachment and to specify a filename. You can set any name you want with any extension.

*Secondly, you have to specify the mime-type of the reader. Set it to "binary/octet-stream". I'm not sure parameter "format" as an influence.

  • Using a Serializer and an Action
<map:pipeline>
  <map:match pattern="save/*">
   <map:act type="header">
     <map:parameter name="Cache-Control" value="Pragma:no-cache" />
     <map:parameter name="Content-Disposition" value="attachment ; filename={1}.xml" />
   </map:act>
   <map:generate type="file" src="{1}.xml" />
   <map:serialize type="xml"/>
  </map:match>
</map:pipeline>

*Generate a document (from the default generator)
*Set the Response object with the action HttpHeaderAction (included in Cocoon classes).
*Serialize the flow (with the xmlSerializer).

This works with IE, mozilla and firebird. Be careful to remove your cache and to reload your browser. If it's not working reopen you're browser.

Lionel Crine

  • No labels