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:

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

Then hook the upload URL to a flow function:

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

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

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);
        cocoon.sendPage("success.html");
      }catch (Exception) {
	cocoon.sendPage("failure.html");
      }
   }

...and register it in the sitemap:

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

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-list>
        <role name="org.apache.cocoon.components.upload.FileUploadManager"
                shorthand="upload_manager"
                default-class="org.apache.cocoon.components.upload.FileUploadManagerImpl"/>
</role-list>

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:

<upload_manager>
    <uploadfolder>/some/where</uploadfolder>
</upload_manager>

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 build.properties or local.build.properties).

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 FileUploadManager.java (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 http://issues.apache.org/bugzilla/show_bug.cgi?id=24529

Attachment: cocoon-upload.jar

Attachment: messageboard.jsp

Attachment: catalog2.jsp

  • No labels