This functionality was introduced in April 4, 2003 and is available in all 2.1 releases. See FileUploadsWithCocoon2.1 for a general overview of uploading, and links to Wiki pages explaining how to upload other than using flow.

Note that in Cocoon 2.1.6 CForms includes an upload widget which makes this easier - see the upload sample in the CForms Block Samples. That sample works on text files only but this is limited by the sample, not by the upload widget.

Start with the following page:

    <form action="upload" method="POST" enctype="multipart/form-data">
     <input type="file" name="upload-file"/>
     <input type="submit" name="action" value="Upload"/>

Then hook the upload URL to a flow function:

   <map:match pattern="upload">
    <map:call function="upload"/>

   <map:match pattern="*.html">
    <map:read src="{1}.html"/>

Then write the flow function, let's call it 'upload.js':

   var role = 'org.apache.cocoon.components.upload.FileUploadManager';

   function upload() {
      var uploader = cocoon.getComponent(role);
      var part = cocoon.request.get("upload-file");
      try {
        uploader.upload (part);
      }catch (Exception) {

...and register it in the sitemap:

  <map:flow language="javascript">
    <map:script src="upload.js" />

To get this example to work as written, you will need a FileUploadManager component that does what the flowscript is asking it to do. Here are the steps to set this up:

1) Grab the cocoon-upload.jar file attached to this page (see below), and drop it into your WEB-INF/lib directory.

2) Create WEB-INF/user.xroles with these contents:

<?xml version="1.0" encoding="UTF-8"?>
        <role name="org.apache.cocoon.components.upload.FileUploadManager"

3) Change the <cocoon> element in WEB-INF/cocoon.xconf to read:

<cocoon version="2.1" user-roles="/WEB-INF/user.xroles">

4) ...and add this to your cocoon.xconf:


5) Set the enable-uploads parameter in WEB-INF/web.xml to true (this is already done for you if you built Cocoon with 'cocoon.enable-uploads=true' in or

Restart Cocoon, and you're on your way.

This example calls the upload() method of the component... but this FileUploadManager component also has some other methods, for controlling the directory into which files are uploaded, etc. Take a look at the interface in (from the .jar file) to learn about those.

EC: The cocoon-upload.jar has been replaced and should be functional. To rebuild it, please see the initial upload component

Attachment: cocoon-upload.jar

Attachment: messageboard.jsp

Attachment: catalog2.jsp

  • No labels