cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Josep Riudavets" <jriudave...@uoc.edu>
Subject Re: Uploading fiiles: flow function
Date Mon, 27 Oct 2003 16:47:17 GMT
Hi again Geoff...

I'm still trying to implement the code in order to upload files with cocoon.

I have readed wiki web a thousand times ... ;-) ..

well... there's an example in wiki web,
http://wiki.cocoondev.org/Wiki.jsp?page=FileUploadWithAction, for uploading
files with action, but it only works on cocoon 2.0,
Example of an Action to get the uploaded file

import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.cocoon.acting.Action;
import org.apache.cocoon.environment.Redirector;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.cocoon.environment.ObjectModelHelper;
import org.apache.cocoon.environment.Request;
import org.apache.cocoon.components.request.multipart.FilePart;
import org.apache.cocoon.components.request.multipart.FilePartFile;

import java.util.Map;
import java.util.Collections;
import java.io.File;

public class UploadTest
  extends AbstractLogEnabled
  implements Action, ThreadSafe
{
    public Map act(Redirector redirector, SourceResolver resolver,
                   Map objectModel, String source, Parameters par)
        throws Exception
    {
        Request request = ObjectModelHelper.getRequest(objectModel);
        FilePart filePart = (FilePart) request.get("uploaded_file");

        File file = ((FilePartFile)filePart).getFile();
        getLogger().debug("Uploaded file = " + file.getCanonicalPath());
        // here you can open an InputStream on the file or whatever
        // you may also want to delete the file after using it
        return Collections.EMPTY_MAP;
    }
}

In the case of cocoon 2.1, there's an example using flow function , but it
is very complex. There is also a tip for using the same action used in
cocoon 2.0, but with some modifications:
http://wiki.cocoondev.org/Wiki.jsp?page=FileUploadsWithFlow

import org.apache.cocoon.servlet.multipart.*;

  ...

  Request request = ObjectModelHelper.getRequest(obj);
  if (request instanceof MultipartHttpServletRequest) {
    Part part = (Part) request.get("blah");
    if (part != null) {
      // do something with it
    } else {
      // parameter not found
    }
  } else {
    // upload is disabled
  }


But ...  i don't know how to implement these modifications ... Could you
tell me what is the exact code for the action with cocoon 2.1 ???

Thanks  a lot, Geoff ... and everybody who can help me !!! ;-)



----- Original Message -----
From: "Geoff Howard" <cocoon@leverageweb.com>
To: <users@cocoon.apache.org>
Sent: Friday, October 24, 2003 5:04 PM
Subject: Re: Uploading fiiles: flow function


> Josep Riudavets wrote:
> > Hi Geoff....
> >
> > I have been trying to develope a system to upload a file to cocoon (I'm
> > using cocoon 2.1) ... but I have not achieved it !!!
>
> Tell me more about what you have tried so far and exactly what you want
> to accomplish.
>
> > I need some help, because I don't understant wiki explanation. I know
flow
> > is not very easy to implement ... but can it be done with an action?
>
> Flow is not that hard.  The point I was making was the wiki mentions an
> upload handler that doesn't exist (because everyone wants to do
> something different with their upload).  You don't have to do it with a
> component lookup to an upload handler.  Let me know what you want to do
> and I can give you a quick example which will work and advice on how to
> improve it.  Do you just want to save it to a directory on disk?
>
> This can be done with an action but not with an existing action.  I'd
> really recommend going with flow.
>
> > Could somebody explain me more about uploading files in Cocoon 2.1 ????
>
> The big thing to remember is you are not uploading them.  Cocoon has
> already handled all the work for you and gives you a way to get the
> contents of the file as an InputStream (or potentially as a
> java.io.File) reference.  Both of those will be gone automatically at
> the end of the request though unless you do something with them, as you
> are trying to do now.
>
> Geoff
>
> >
> > ----- Original Message -----
> > From: "Geoff Howard" <cocoon@leverageweb.com>
> > To: <users@cocoon.apache.org>
> > Sent: Wednesday, October 15, 2003 1:52 AM
> > Subject: Re: Uploading fiiles: flow function
> >
> >
> >
> >>Josep Riudavets Aguilo wrote:
> >>
> >>
> >>>Hi all ...
> >>>
> >>>I'm delevoping some code in Cocoon 2.1 in order to upload files.
> >>>
> >>>I have added these lines into my XSL page:
> >>>
> >>> <form action="/upload" method="POST" enctype="multipart/form-data">
> >>>     <input type="file" name="blah"/>
> >>>     <input type="submit" name="action" value="Upload"/>
> >>>    </form>
> >>>
> >>>Next, I have added this code into the sitemap.xmap:
> >>>
> >>>   <map:match pattern="/upload">
> >>>    <map:call function="upload"/>
> >>>   </map:match>
> >>>
> >>>Now ... I have to write next function:
> >>>
> >>>   var role = Packages.org.mystuff.UploadManager.ROLE;
> >>>
> >>>   function upload() {
> >>>      var uploader = cocoon.componentManager.getComponent(role);
> >>>      var part = cocoon.request.get("blah");
> >>>      var success = uploader.handle(part.getInputStream());
> >>>      if (success) {
> >>>sendPage("success.html");
> >>>      } else {
> >>>sendPage("failure.html");
> >>>      }
> >>>   }
> >>>
> >>>But I don't know where to write this function !!! Is is a javascript
> >>>function???
> >>>
> >>>(all info is from
> >>>http://wiki.cocoondev.org/Wiki.jsp?page=FileUploadsWithFlow)
> >>
> >>It's flowscript which, yes, is implemented in Cocoon using the
> >>javascript core from the Rhino project.  You'll need to read up at least
> >>a little on the wiki about flow before making much sense of this.  That
> >>was an example snippet, not a cut-and-paste ready solution.
> >>
> >>For instance, it presupposes that you have a component written and
> >>deployed in Cocoon called org.mystuff.UploadManager and you also have a
> >>pipeline which matches "success.html" and "failure.html".  oh, and it
> >>presupposes that org.mystuff.UploadManager has a public method
> >>handle(InputStream is).
> >>
> >>I think this is going to be so common that it would probably deserve to
> >>be in the core at least in a simple form.  I'll see if I can make some
> >>time for it.  This is really pretty easy and would be a good simple
> >>patch for someone who is interested.  I'd be happy to help out.
> >>
> >>Geoff
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
> For additional commands, e-mail: users-help@cocoon.apache.org
>
>
>



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@cocoon.apache.org
For additional commands, e-mail: users-help@cocoon.apache.org


Mime
View raw message