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/08/02 15:01:46 UTC

[Cocoon Wiki] Updated: FileUploadsWithFlow

   Date: 2004-08-02T06:01:46
   Editor: JaanekOja <ja...@jawilla.net>
   Wiki: Cocoon Wiki
   Page: FileUploadsWithFlow
   URL: http://wiki.apache.org/cocoon/FileUploadsWithFlow

   1076487328

Change Log:

------------------------------------------------------------------------------
@@ -1,4 +1,9 @@
-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.
+''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.
 
 Start with the following page:
 {{{
@@ -20,35 +25,25 @@
    </map:match>
 }}}
 
-Then write the flow function, let's call it 'upload.js':
+Then write the flow function:
 {{{
    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");
+      var success = uploader.upload (part);
+      if (success) {
+	sendPage("success.html");
+      } else {
+	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.  So first, grab the cocoon-upload.jar file attached to this page (see below), and drop it into your WEB-INF/lib directory.
 
-
-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:
+Next, you need to register this as a managed component.  Create WEB-INF/user.xroles with these contents:
 {{{
 <?xml version="1.0" encoding="UTF-8"?>
 <role-list>
@@ -58,29 +53,36 @@
 </role-list>
 }}} 
 
-3) Change the <cocoon> element in WEB-INF/cocoon.xconf to read:
+Finally, 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:
+Restart Cocoon, and you're on your way.
 
+[[BR]]
+----
+Alternatively, you can write the following java code in your sitemap components:
 {{{
-<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.
+  import org.apache.cocoon.servlet.multipart.*;
 
-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.
-
-----
-I am not too sure but I think the attached file cocoon-upload.jar is not the right file. It's too small... Seems to be wrong... I used an older file which I found in the history of this page. It does work.
+  ...
 
+  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
+  }
+}}}
 [[BR]]
 [[BR]]
 '''Attachment:''' attachment:cocoon-upload.jar [[BR]]
-
+'''Attachment:''' attachment:messageboard.jsp [[BR]]
+'''Attachment:''' attachment:catalog2.jsp [[BR]]