cocoon-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Marco Rolappe" <>
Subject AW: File upload
Date Thu, 13 Nov 2003 13:47:52 GMT
hi jan,

> -----Urspr√ľngliche Nachricht-----
> Von:
> []Im
> Auftrag von Jan Wielgus
> Gesendet: Donnerstag, 13. November 2003 10:15
> An:
> Betreff: File upload
> Hello,
> I would like to get the file upload working with cocoon 2.1, but
> unfortunately the examples in wiki don't give enough for such newbie
> as me :( In the wikipage about upload with flow written by Geoff
> Howard there are two possibilities showed. The first one, with
> JavaScript contains the following line:
> var role =;
> The problem is, I don't know, where I should take the ROLE object
> from? I actually get stuck at this moment, when I want to do
> something here...

the UploadManager in this example is a custom (Avalon) component. if you
don't know about Avalon components, containers, roles, etc. you might want
to read about that first (that is, if you want to go the component way).

> The second possibility says "Alternatively, you can write the
> following java code in your sitemap components". What does it
> actually mean here? An Action? If yes, is it possible to get the
> complete code of that action?
> Or maybe someone has a working example of a file upload? Any help
> would be really appreciated.

following short example is for cocoon 2.0.4, but I guess it should also work
for 2.1.x:

public class MyUploadAction
	extends AbstractLogEnabled
	implements Action

	/* (non-Javadoc)
	 * @see
, org.apache.cocoon.environment.SourceResolver, java.util.Map,
java.lang.String, org.apache.avalon.framework.parameters.Parameters)
	public Map act(
		Redirector redirector,
		SourceResolver resolver,
		Map objectModel,
		String source,
		Parameters par)
		throws ParameterException
// the action parameter 'parameter-name' specifies the request parameter
name the uploaded file is bound to
		paramName = par.getParameter("parameter-name");

// get the request
		request = ObjectModelHelper.getRequest(objectModel);

// get the FilePart (handle to the file) from the request parameter
		filePart = (FilePart) request.get(paramName);

// ok, file was successfully uploaded
		if (filePart != null)
// get the file
			file = ( (FilePartFile) filePart).getFile();

			getLogger ().debug ("uploaded file: " + file);

// signal 'success' to sitemap
			return (Collections.EMPTY_MAP);
			getLogger ().warn ("upload failed");

// signal 'failure' to sitemap
			return (null);

for uploads to work (correctly), some things have to be configured, most
importantly a supporting RequestFactory, e.g. the MultiPartRequestFactory.
have a look at web.xml:

This parameter allows you to select the request factory. Possible choices
are as follows:
  - org.apache.cocoon.components.request.MultipartRequestFactoryImpl
  - org.apache.cocoon.components.request.MaybeUploadRequestFactoryImpl


then have a form post to an URL:

	<form action="upload" method="post" enctype="multipart/form-data">
		<input type="file" name="my-uploaded-file"/>

		<input type="submit"/>

and handle the request in your sitemap:

	<map:match pattern="upload">
		<map:act type="upload">
			<map:parameter name="parameter-name" value="my-uploaded-file"/>

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message