You are viewing a plain text version of this content. The canonical link for it is here.
Posted to docs@cocoon.apache.org by do...@cocoon.apache.org on 2004/11/17 12:01:53 UTC

[Cocoon Wiki] Updated: FileUploadsWithFlow

   Date: 2004-11-17T03:01:52
   Editor: NicolasMaisonneuve <ni...@free.fr>
   Wiki: Cocoon Wiki
   Page: FileUploadsWithFlow
   URL: http://wiki.apache.org/cocoon/FileUploadsWithFlow

   1076953380

Change Log:

------------------------------------------------------------------------------
@@ -1,9 +1,4 @@
-''This is taken from [http://marc.theaimsgroup.com/?t=104946920900004&r=1&w=2 this thread] in which [:Stefanomazzocchi] describes the changes to the file upload system.''
-
-''I'm not sure if this totally replaces the old file upload stuff at FileUploadsWithCocoon and FileUploadWithAction, so once this gets straightened out, expect a centralized file uploads page.'' -- TonyCollen
-----
-
-This functionality was introduced in April 4, 2003 and is available in all 2.1 releases.
+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.
 
 Start with the following page:
 {{{
@@ -25,25 +20,35 @@
    </map:match>
 }}}
 
-Then write the flow function:
+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");
-      var success = uploader.upload (part);
-      if (success) {
-	sendPage("success.html");
-      } else {
-	sendPage("failure.html");
+      try {
+        uploader.upload (part);
+        cocoon.sendPage("success.html");
+      }catch (Exception) {
+	cocoon.sendPage("failure.html");
       }
    }
 }}}
 
-To get this example to work as written, you will need a !FileUploadManager component that does what the flowscript is asking it to do.  So first, grab the cocoon-upload.jar file attached to this page (see below), and drop it into your WEB-INF/lib directory.
+...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.
 
-Next, you need to register this as a managed component.  Create WEB-INF/user.xroles with these contents:
+2) Create WEB-INF/user.xroles with these contents:
 {{{
 <?xml version="1.0" encoding="UTF-8"?>
 <role-list>
@@ -53,34 +58,25 @@
 </role-list>
 }}} 
 
-Finally, change the <cocoon> element in WEB-INF/cocoon.xconf to read:
+3) Change the <cocoon> element in WEB-INF/cocoon.xconf to read:
 {{{
 <cocoon version="2.1" user-roles="/WEB-INF/user.xroles">
 }}}
 
-Restart Cocoon, and you're on your way.
+4) ...and add this to your cocoon.xconf:
 
-[[BR]]
-----
-Alternatively, you can write the following java code in your sitemap components:
-{{{
-
-  import org.apache.cocoon.servlet.multipart.*;
-
-  ...
-
-  Request request = ObjectModelHelper.getRequest(obj);
-  if (request instanceof MultipartHttpServletRequest) {
-    Part part = (Part) request.get("upload-file");
-    if (part != null) {
-      // do something with it
-    } else {
-      // parameter not found
-    }
-  } else {
-    // upload is disabled
-  }
+{{{
+<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.
+
 [[BR]]
 [[BR]]
 '''Attachment:''' attachment:cocoon-upload.jar [[BR]]