Friday 11 April 2014

Upload files and access form elements in a custom Portlet - Using multipart/form-data and UploadPortletRequest

This blog post will help you if you are looking to add form upload functionality to your JSP form and then access it in your controller, java class.

For this blog post I have used Liferay portal.

Lets get started. In your Portlet JSP please add enctype="multipart/form-data" to the form tag.

The complete JSP code is provided below for reference




Next lets go to our controller class. Once the form is submitted this class will come into picture.

First we need to get UploadPortletRequest object using the ActionRequest object. This is the object we will use to access all the form elements.

UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(request);

String docTitle = ParamUtil.getString(uploadRequest, "docTitle");
String comments= ParamUtil.getString(uploadRequest, "docComments");

I have used Liferay's ParamUtil which is a helper class. But you can directly get these parameters from the request object using request.getParameter();

Also you can extract file details using

String fileName = uploadRequest.getFileName("documentFile");
InputStream inputStream = uploadRequest.getFileAsStream("documentFile");

That's it.

No comments:

Post a Comment