Provides a file upload component for Tapestry based on Jakarata Commons FileUpload.
This is packaged as an add-on to Tapestry because of the number of additional dependencies it adds. However, Maven takes care of that for you!
The upload component supports default value binding (based on id) and validation.
<t:form> <t:errors/> <input t:type="upload" t:id="file" validate="required"/> <br/> <input type="submit" value="Upload"/> </t:form>
Here, because the value parameter was not bound, the component used the file property of its container (because the component's id is 'file'). If you want to upload as a different property, either bind the value parameter or change the component's id.
public class UploadExample { @Property private UploadedFile file; public void onSuccess() { File copied = new File("/my/file/location/" + file.getFileName()); file.write(copied); } }
In some cases, file uploads may fail. This can be because of a simple communication exception, or more likely, because the configured maximum upload size was exceeded.
When a file upload exception occurs, Tapestry will trigger a "uploadException" event on the page to notify it of the error. All other normal processing is skipped (no "activate" event, no form submission, etc.).
The event handler should return a non-null object, which will be handled as a navigational result. Example:
@Persist(PersistenceConstants.FLASH) @Property private String message; Object onUploadException(FileUploadException ex) { message = "Upload exception: " + ex.getMessage(); return this; }
Note the importance of return this;. A void event handler method, or one that returns null, will result in the FileUploadException being reported to the user as an uncaught runtime exception.
Four values may be configured as symbols:
The class UploadSymbols defines constants for all four of these.
The Commons FileUpload library uses the CommonsIO file cleaner service to remove temporary files when they are no longer needed. This service creates a thread to carry out its work. If the commons-io library is shared amongst multiple applications (e.g. added to server classpath) it is possible for an application to terminate this thread prematurely and cause errors for the other applications. (see the Resource Cleanup section in for more discussion)
Technically the file cleanup service is not needed by Tapestry Upload (which deletes temporary files at the end of request processing). However it is currently not possible to disable it (enhancement request has been filed as FILEUPLOAD-133).