Saturday, 29 June 2013

Custom Portlet for File Upload in database in Liferay.

Hi friends this time i am going to show you how to create custom portlet that can upload file in database.Follow this steps and you will see that your portlet is uploading files.


1)open service.xml and write down the following entity

<entity name="FileUploader" table="fileuploader" local-service="true">
<column name="fid" type="long" primary="true"/>
    <column name="content" type="String"/>


2)Now do Ant-build service for the portlet you created


1)Open portlet-model-hints.xml and you will see a code like this

<model name="com.sample.model.FileUploader">
<field name="fid" type="long" />
<field name="content" type="String" />


2)Now plz update the above code with this below code

          <model name="com.sample.model.FileUploader">
           <field name="fid" type="long" />
           <field name="content" type="String">
                     <hint-collection name="CLOBTYPE" />
           <hint-collection name="CLOBTYPE">
                      <hint name="max-length">2000000</hint>

3)Again do Ant-build service and you will see you the sql script updated file.


1)Now open view.jsp an add following code to insert file

<portlet:actionURL name="ext" var="actionFoo"></portlet:actionURL>
<form action="<%=actionFoo.toString()%>" method="post" enctype="multipart/form-data">

 <input type="file" name="file"/>
 <input type="submit" value="Submit" />


2)Now deploy it and you will see the file and the submit button in your page.

1)Open and add following code

public void add(File file)
byte[] bt;
long fid=0L;
try {
FileUploader f = fileUploaderLocalService.createFileUploader(fid);
bt = FileUtil.getBytes(file);
fileUploaderPersistence.update(f, false);

} catch (IOException | SystemException e) {
// TODO Auto-generated catch block
2)Now Ant-build service and open 

1)Open action class and write down logic as shown below

public void ext(ActionRequest arq,ActionResponse ars)
UploadPortletRequest uploadrequest=PortalUtil.getUploadPortletRequest(arq);
File file=uploadrequest.getFile("file");


2)Now again Build service and compile the whole the java class .
3)Now deploy and if you have done correctly you will see your portlet is being deployed successfully.
4)Now open your page and upload file and you will see that file is being inserted in your database.

If any query plz do comment or mail us at: