Letting users upload a file is an often-requested feature, but it never seems easy to implement. Although Tomahawk doesn’t have the ultimate solution, its<t:inputFileUpload>
component does make our life a bit easier. Let’s have a look at how we can use this component in our MIAS application. Assume that we have to provide the capability to upload a photo for each kid in the system, to make it easier to identify the kids. Now, how do we approach this?
First, we need to expand our backing bean (EditKidForm.java
) to be able to receive image files and store them somewhere. We add a private variable to store the uploaded file:
private UploadedFile photoFile;
The variable is of the type org.apache.myfaces.custom.fileupload.UploadedFile
, a type provided by Tomahawk. Of course, we also add a getter and setter method for the photoFile
. When the form is submitted, we need to persist the image file. The most efficient way to store a binary file is on a file system, so let’s store...