Sunday, June 26, 2011

Extending Web Form Portlet – Adding File Upload Control

In Liferay Portal, Web Form Portlet is used to create custom forms but the types of field that can be created are limited. One such missing field type is File Upload Control. It’s a common requirement to have file upload control in forms.


Following are the steps to add File Upload Control to Web Form Portlet -


1.  Add a new field type to edit_field.jsp

<aui:option selected='<%= fieldType.equals("file") %>' value="file"><liferay-ui:message key="file" /></aui:option>

2.  Accordingly update the view.jsp to render the your new field

<c:when test='<%= fieldType.equals("file") %>'>

<aui:input cssClass='<%= fieldOptional ? "optional" : StringPool.BLANK %>' label=" <%=HtmlUtil.escape(fieldLabelDisplay) %>" name="<%= fieldName %>" type="file" value="<%=HtmlUtil.escape(fieldValue) %>"/>

</c:when>

3.  Set the preferences for this field in ConfigurationActionImpl.java

boolean isFileUpload = false;

if ("file".equals(fieldType)) {

isFileUpload = true;

}

preferences.setValue("isFileUpload", String.valueOf(isFileUpload));

4.  Finally get the file in WebFormPortlet.java and proceed with your implementation…

File file = uploadRequest.getFile(paramName.toString());

Proceed we the implementation with file object for example save to document library, send in mail etc.

 

 

31 comments:

  1. [...] um post com algo relacionado a modificar o webform para colocar um campo de file upload aqui [...]

    ReplyDelete
  2. Hi there, I discovered your website by the use of Google whilst looking for a comparable topic, your web site got here up, it appears great. I've bookmarked it in my google bookmarks.

    ReplyDelete
  3. Fine information, I will be checking back again frequent to look around for refreshes.

    ReplyDelete
  4. Thanks for liking the stuff...

    ReplyDelete
  5. I'll try to keep it updated and continue to provide latest and meaningful contents...

    ReplyDelete
  6. Wonderful document along with very easy for you to figure out justification. Exactly how can My spouse and i attempt receiving concur for you to submit element in the document inside my future news letter? Getting suitable credit history to you personally your publisher along with web page link on the blog won't certainly be a trouble.

    ReplyDelete
  7. Can you post example of your WebFormPortlet.java or at least part responsible for file handling ?

    ReplyDelete
  8. I consider this article one of the most thought-provoking articles I've read over time. Your views and ideas are both unique and informative I've learned something through this information.

    ReplyDelete
  9. I'm happy I found this blog, I think it's the most useful on the matter so far. I also run a site and if you want to ever get serious in a little bit of guest writing for me, feel free to let me know, i'm always looking for people to check out my site. Stop by and leave a comment sometime!

    ReplyDelete
  10. Superb post and additionally simple to make sure you comprehend description. Exactly how can Document keep performing obtaining authorization to make sure you publish part for the guide into my approaching e-newsletter? Providing correct credit scores in your direction all the creator and additionally url to website wouldn't deemed a dilemma.

    ReplyDelete
  11. couldnt find where to put that line (Step 4)

    ReplyDelete
  12. Anywhere in your portlet class where you want to make use of the uploaded file

    ReplyDelete
  13. hi there, i couldn't find where to put the number 3 lines...pls help.thank you

    ReplyDelete
  14. hi there, i need to have a text field appear once a radio button is pressed, is it possible?if it is possible, please let me know how can i do this...thank you..

    ReplyDelete
  15. Cathy

    Its pretty easy to implement. just have radio button and text box on the form. Use onClick event of radio button to manage the visibility of textbox.

    ReplyDelete
  16. In the ConfigurationActionImpl.java where other preferences are being persisted

    ReplyDelete
  17. oh thank.I'll try about that..so far liferay web form portlet is very useful to me.

    ReplyDelete
  18. hi, im having trouble with the same problem of cathy about radio button, i tried to put 3 radio buttons ,there appear a textbox once the radio button is click but the problem is it applies to all web form portlet that i have.how can i limit it a particular portlet.looking forward to your response.thank u.

    Bexx

    ReplyDelete
  19. Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog and posts. Anyway I’ll be subscribing your feed and I hope to read your blog again.

    ReplyDelete
  20. Pretty good post. I just stumbled upon a webpage not to mention wanted towards state who I have truly enjoyed reading a website not to mention articles. Anyway I’ll turn out to be subscribing a give food not to mention I just desire towards read a website yet again.

    ReplyDelete
  21. [...] can get here.Could you create new thread for new post. Already some discussions are going on. Create new one [...]

    ReplyDelete
  22. Hi,

    When I try to get the file using this line,

    File file = uploadRequest.getFile(paramName.toString());

    uploadRequest could not be resolved.

    Do I have to create my own method (uploadRequest) inside WebFormPortlet.java?

    Or should this line work by itself?

    Thank you in advance for any feedback.

    ReplyDelete
  23. What's Happening we are not used to this, I stumbled upon this Ive think it is absolutely helpful and possesses helped me out loads. Hopefully to contribute & assist other users like its solved the problem. Well done.

    ReplyDelete
  24. Hi...thnx for the great post....
    i am unable to pass "file" object in ur last point no.4.
    kindly tell me where to place file object....i mean where to put this code...????

    thnx !!

    ReplyDelete
  25. Hi Aira !!

    did u get the solution for this...?? if yes, plz let me know...

    thnx !!!!

    ReplyDelete
  26. Hi,

    Has anyone figured out how to use:
    File file = uploadRequest.getFile(paramName.toString());

    I use it on the SaveData method in WebFormPortlet but I get null.

    ReplyDelete
  27. Step 4 is a bit vague. Anyone have a concrete example?

    ReplyDelete
  28. please write whole of ConfigurationActionImpl.java here.tnx

    ReplyDelete
  29. Hello There ,
    Its very good post.I have used it but I am stuck at step 4 where i have written following code snippet to save uploaded file to document library.But it does not save my file in Test folder i have created. Please reply.


    UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(actionRequest);
    File file = uploadPortletRequest.getFile("file");

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
    ServiceContext serviceContext = ServiceContextFactory.getInstance(actionRequest);


    String contentType = MimeTypesUtil.getContentType(file);
    InputStream inputStream = new FileInputStream(file);

    Folder folderName = DLAppLocalServiceUtil.getFolder(parentRepositoryId, parentFolderId, "Test");
    long folderId = folderName.getFolderId();
    long repositoryId = folderName.getRepositoryId();


    FileEntry fileEntry = DLAppLocalServiceUtil.addFileEntry(themeDisplay.getUserId(),
    repositoryId, folderId, file.getName(),
    contentType, file.getName(), "description",
    "changeLog", inputStream, file.length(), serviceContext);

    ReplyDelete
  30. Very good post.

    In view.jsp file, add enctype=”multipart/form-data” in aui:form

    ReplyDelete