Hi,

You should be able to get the FilePart object representing the uploaded file
using request.get("upload_field"). If the file was stored into the upload dir 
(which is enabled by default) you can cast the FilePart to a FilePartFile 
from which you can get the java.io.File using getFile(). All other 
information on the file you can get from the the headers 
(FilePart.getHeaders()).

I've attached a sample action to move the file to another directory as
described above. 

To enable it add this to the top sitemap (above the xsp/* pipeline):

   <map:match pattern="xsp/upload">
    <map:act type="file-upload">
     <map:parameter name="upload-dir" value="myuploaddir" />
     <map:generate src="docs/samples/xsp/upload.xsp" type="serverpages" />
     <map:transform src="stylesheets/dynamic-page2html.xsl" />
     <map:serialize />
    </map:act>
   </map:match>

Note that the upload dir is created rin the webapps/cocoon dir instead of 
work/cocoon/cocoon-files.

I've tested this code against the current CVS (cocoon 2.1-dev branch). It will
probably not work with 2.0.2 since that release contained a bug 
(http://nagoya.apache.org/bugzilla/show_bug.cgi?id=7510)

Greets,
        Jeroen

On Saturday 13 April 2002 02:41 am, you wrote:
> Hi all,
>
> I've been mucking around with some file upload stuff and noticed a change
> in behaviour since Joren's replacement for maybeupload was done back in
> Feb. Before the change it was possible to still get hold of the reference
> to the uploaded file from the request object in an action thus enabling you
> to rewrite the file anywhere e.g. into a db BLOB.  This is now not easily
> possible since the request does not contain the right stuff any more.  I
> note from the archives that when the code was submitted the suggestion was
> that control could be passed to an action to determine how and where to
> write the file but the final code is driven by the web.xml parameters.
>
> I would really like to have the ability to chose as often as each request
> where the uploaded file is to be put and will have a more detailed look at
> the code over the weekend to see what might need doing.  To me it seems
> there are a number of possible choices:
>
> 1. a small patch to the MultipartRequest stuff that leaves the required
> info in the request and then an action to do an upload to a place specified
> via a param
> 2. an alternative MultipartRequest handler
> 3. a complex action that works if SimpleRequestHandler is used and not
> Multipart - i.e. shift the work along (urgh?)
> 4. something else maybe
>
> Just interested on soliciting some thoughts on this whilst I tip tappity
> away!
>
> Cheers
>
> Jeremy
>
>
>
>
> _________________________________________________________
> Do You Yahoo!?
> Get your free @yahoo.com address at http://mail.yahoo.com
/*****************************************************************************
 * Copyright (C) The Apache Software Foundation. All rights reserved.        *
 * ------------------------------------------------------------------------- *
 * This software is published under the terms of the Apache Software License *
 * version 1.1, a copy of which has been included  with this distribution in *
 * the LICENSE file.                                                         *
 *****************************************************************************/

package org.apache.cocoon.acting;

import org.apache.avalon.framework.configuration.Configurable;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.avalon.framework.thread.ThreadSafe;
import org.apache.cocoon.Constants;
import org.apache.cocoon.environment.Redirector;
import org.apache.cocoon.environment.Request;
import org.apache.cocoon.environment.Context;
import org.apache.cocoon.environment.SourceResolver;
import org.apache.cocoon.acting.AbstractConfigurableAction;

import org.apache.cocoon.components.request.multipart.*;

import java.util.*;
import java.io.*;

public class UploadAction extends AbstractConfigurableAction implements ThreadSafe  {

  Properties default_properties = null;
	

  public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception {
    HashMap results = new HashMap();
      Request request = (Request)objectModel.get(Constants.REQUEST_OBJECT);
      Context context = (Context)objectModel.get(Constants.CONTEXT_OBJECT);
      Properties properties = new Properties(default_properties);
      byte[] buf=new byte[4096];

      Enumeration enum=request.getParameterNames();
      while(enum.hasMoreElements()) {
		String name=(String)enum.nextElement();

		Object obj=request.get(name);
		getLogger().debug(request.getClass().getName());

		if (obj instanceof FilePart) {
			getLogger().debug("Uploading file: "+((FilePart)obj).getFileName());
   	     	
   	     		String fileName=((FilePart)obj).getFileName();
   	     		String uploadDir=(String)parameters.getParameter("upload-dir");
			String realPath=context.getRealPath("/");
		
			if (realPath!=null)
				uploadDir=realPath+uploadDir;	   	     
	
   	     		File dir=new File(uploadDir);
   	     		if (!dir.exists())
   	     			dir.mkdir();
   	     
			if (obj instanceof FilePartFile) {
				((FilePartFile)obj).getFile().renameTo(new File(uploadDir+File.separator+fileName));

			} else {	
	
	   	     		FileOutputStream out=new FileOutputStream(uploadDir+File.separator+fileName);
   		     		InputStream in=((FilePart)obj).getInputStream();
   	     			int read=in.read(buf);
   	     			while(read>0) {
   	     				out.write(buf,0,read);
   	     				read=in.read(buf);
   	     			}
   	     	
   	     			out.close();
			}			
			
		} else if (obj instanceof String) {
			getLogger().debug("Skipping parameter: "+(String)obj);
		}			

      }

      return Collections.unmodifiableMap(results);

  }
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, email: [EMAIL PROTECTED]

Reply via email to