File Upload HOWTO

Weaver File Upload How To

This document describes how to deal with file uploading in Weaver.

Intended Audience

This document is aimed at developers writing a Weaver based application that involves uploading files to the server.

Prerequisites

You should have a good understanding of Weaver and writing applications using the Weaver framework.

Steps

Weaver makes simple the tricky job of dealing with forms that contain a file upload input control, or indeed several such controls mixed with other input fields.

Most of the work is handled transparently, all you need to do is to:

  1. Write an HTML page containing an appropriate form.
  2. Pass details of the uploaded files through to your W-Bean via the WAD file.
  3. Code the W-Beans to process the uploaded files.

The HTML Form

For Weaver to recognise that your HTML form should undergo the special processing required to support file upload, it must declare its encoding type to be multipart/form-data. This is achieved using the enctype attribute to the form tag as follows:

<form method="POST" action="whatever" enctype="multipart/form-data"/>

Your form may then include any of the usual HTML input variants, including that for uploading files - that is type="FILE":

<input type="FILE" name="myname"/>

Typically a file input control will be rendered by the Web browser in a way that allows the user to select a file for upload.

A more complete example might be:

<form action="contentManager" method="post"> <input type="hidden" name="event" value="processFileUpload"/> Enter a comment <input name="comment"/> <input type="FILE" name="file"/> <input type="submit" value="Upload File"/> </form>

All of the "normal" input fields are available in the WAD file and in W-Beans in the usual way - you do not need to do anything special. In addition - as we will see in the following sections - any uploaded files will be available in the EL and to your W-Beans.

In the WAD file

Any uploaded files are available via the WeaverRequestContext in W-Beans code.

However, as with other values supplied from the View it makes sense to add a level of indirection to decouple the View and the Model.

Uploaded files are available in the EL via the uploadedFile scope. For example:

... <call name="processFile"> <param name="event" value="${param.event}"/> <param name="userText" value="${param.comment}"/> <param name="uploadedFile" value="${uploadedFile.file}"/> </call> ...

All objects in the uploadedFile scope are of type com.oldlight.weaver.context.WeaverUploadedFile. Please see the javadoc for details.

Accessing Uploaded Files in W-Beans

As described above uploaded files are made available via the WeaverRequestContext object, but are better passed in to your W-Bean's WeaverBeanContext by the EL in the WAD file.

Uploaded files are then accessible like any other param, for example:

... public void handle(WeaverExecutionContext context) throws WeaverClientException { ... WeaverUploadedFile=file (WeaverUploadedFile)context.getBeanContext().getAttribute("uploadedFile"); ...

As you can see from the above, uploaded files are returned to you in a WeaverUploadedFile object. This object has methods to get a java.io.File object that points to the uploaded file on the local file system and to get the content type (MIME type) of the uploaded file:

... FileReader reader=new FileReader(file.getFile()); ...

Please see the javadoc for details.

You should take careful note that by default Weaver will automatically clean up any uploaded file at the end of a requests life. If you want to keep Weaver's copy of the file around then simply call:

file.setKeep(true);

It is then your responsibility to delete the uploaded file when it is nolonger needed.

Configuration

Weaver stores uploaded files in a temporary directory. This directory is configured in the context wide configuration file (WEB-INF/application/conf/application.properties using the weaver.upload_dir property as follows:

weaver.upload_dir: /WEB-INF/application/uploads