Search code examples
javajsffile-uploadliferayportlet

How to upload a file in JSF portlet in Liferay 6.0.6


I'm developing a JSF 2.0 portlet for Liferay 6.0.6 (Plugins SDK 6.1) and I need file upload functionality. I tried the following different solutions without success:

Any suggestion how to do it is welcomed, also hacks or using other technologies than JSF.


Solution

  • Why not use a standard HTML form so:

    <form action="your_action_goes_here" method="post" enctype="multipart/form-data">
        <input type="file" name="file" id="file" />
        <input type="submit" name="submit" value="Submit" />
    </form>
    

    Then in your Java code override the processAction method (usually in a class that extends GenericPortlet or maybe Liferay's MVCPortlet or JSPPortlet (for 5.2.3)) and then you can get the file itself by:

    public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) {
        UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(actionRequest);
        File file = (File) uploadRequest.getFile("file");
        // Do something with your file here
    }
    

    Job done! :) This is only skeleton code, and there will be exception handling you need to do but your IDE will help with that.

    ~~ EDIT ~~~

    Other possible solution maybe to use:

     HttpServletRequest req = FacesUtil.getRequest();
     UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(req);
    

    This I got from: http://ironicprogrammer.blogspot.com/2010/03/file-upload-in-jsf2.html

    Is that any help?