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.

Service.xml

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"/>

</entity>

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




portlet-model-hints.xml

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

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

</model>
<model-hints>

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

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

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

View.jsp

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" />

</form>


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

FileUploaderLocalServiceImpl.java

1)Open FileUploaderLocalServiceImpl.java and add following code

public void add(File file)
{
byte[] bt;
long fid=0L;
try {
fid=CounterLocalServiceUtil.increment();
FileUploader f = fileUploaderLocalService.createFileUploader(fid);
bt = FileUtil.getBytes(file);
System.out.println(bt);
f.setContent(Base64.objectToString(bt));
fileUploaderPersistence.update(f, false);

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

contoller.java 

1)Open controller.java 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");
System.out.println("sddgvwsg"+file.getName());
FileUploaderLocalServiceUtil.add(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:
chiragmsc007it@gmail.com
mehdisunasara@gmail.com


















No comments:

Post a Comment