Skip to main content

Adding file upload to JSF 2.0

Posted by mriem on September 30, 2011 at 11:26 AM PDT

 Since JSF has been around people have been wanting a full blown file upload component.
While this blog entry will not give you that, it will show you that using the right codebase
uploading files in JSF 2.0 becomes a breeze.

Step 1, add the JSF upload dependency to your web application

        <dependency>
            <groupId>com.manorrock.jsf</groupId>
            <artifactId>com-manorrock-jsf-upload</artifactId>
             <version>2.x.x</version>
        </dependency>

Step 2, create the JSF page with an HTML input component on it.

    <h:form enctype="multipart/form-data">
        File to add: <input size="32" type="file" name="file"/> <br/>
       <h:commandLink action="#{fileAddBean.doAdd}" value="Add"/>
    </h:form>

Step 3, on the server side access the data.

    String fileString = FacesContext.getCurrentInstance().
        getExternalContext().getRequestParameterMap().get("file");

Note the 'fileString' will contain the content as a BASE64 encoded string,
so you will need to BASE64 decode it to get the byte-array.

See http://www.manorrock.com/products/jsf/upload/ for more information

Please be aware, the actual codebase will not become available till about the
first month of 2012 since it needs to go through a release process. If you want
early access let me know!

 

Related Topics >>