O'Reilly logo

PrimeFaces Beginner's Guide by K. Siva Prasad Reddy

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action – using the FileUpload component

Let us see how we can use the FileUpload component to upload the user profile image of type .jpg, .jpeg, .gif, or .png, which should be a maximum of 1 MB:

  1. Create a user account form to upload the user image:
    <h:form enctype="multipart/form-data">
      <p:fileUpload 
        fileUploadListener="#{userController.handleUserPicUpload}" 
        allowTypes="/(\.|\/)(gif|jpe?g|png)$/" 
        invalidFileMessage="Only gif, jpg or png type images are allowed"
        sizeLimit="1048576"
        invalidSizeMessage="File size should not exceed 1MB"/>
    </h:form>
  2. Implement the actionListener method to save the uploaded image:
    public void handleUserPicUpload(FileUploadEvent event) { UploadedFile uploadedFile = event.getFile(); String fileName = uploadedFile.getFileName(); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required