Friday, December 24, 2010

Fileupload using service.xml file

I have done this in MVC portlet. File upload in liferay using service.xml file is bit tricky. With small modification in portlet-model-hints.xml we can achieve this.

Step 1: Give the data type as string to store the uploaded file.

entry in service.xml file

<entity name="FileUploader" table="fileuploader" local-service="true">

<column name="fid" type="long" primary="true"/>

<column name="content" type="String"/>

</entity>

Step 2 : Do 'ant build-service' from specific portlet level. As you know that which is used to generate the api to interact with database.

Step 3 : Open 'portlet-model-hints.xml' file which is in 'WEB-INF/src/META-INF'.

Initially this xml file look like this

<model-hints>

<model name="com.sample.mvc.model.FileUploader">

<field name="fid" type="long" />

<field name="content" type="String" />

</model>

</model-hints>

Depending on this xml file script files will generate which are in 'sql' folder.

Define 'hint-collection' tag for the 'content' column where i am storing the file content in this example.

Modified file will be as shown below.

<model-hints>

<model name="com.sample.mvc.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>

Step 4 : Now do again 'ant build-service' to update the corresponding script files. This will update the sql script file. We can see the datatype of 'content' column in sql script as TEXT. When we deploy it will create CLOB type column in your database.

Step 5 : Write this logic in your action class to get the file from the jsp page and store it into the database.

public void abc(ActionRequest arq,ActionResponse ars) throws Exception {

UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(arq);

FileUploader fileUp = new FileUploaderImpl();

fileUp.setFid(CounterLocalServiceUtil.increment("FileUploader.class"));

File file = uploadRequest.getFile("file");

byte[] bytes = FileUtil.getBytes(file);

fileUp.setContent(Base64.objectToString(bytes));

FileUploaderLocalServiceUtil.addFileUploader(fileUp);

}

Your JSP page something look like this :

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<portlet:defineObjects />

<%@page import="javax.portlet.PortletURL"%>

<portlet:actionURL name="abc" var="actionURL1"></portlet:actionURL>

<form action="<%= actionURL1.toString() %>" method="post" enctype="multipart/form-data">

Name : <input type="text" name="urname" />

<input type="file" name="file"/>

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

</form>

5 comments:

Unknown said...

Hi
Can u tel me How to Get the file from database

Unknown said...
This comment has been removed by the author.
seeya said...

I am getting error for the following line. The method is unimplemented. What is the code for the setContent function ? How should it be implemenetd?
setContent(Base64.objectToString(bytes));

Anonymous said...

I don't have service.xml file, how do I get it?

ASIF AFTAB said...

if we use in more than one place then it is required to write the below code more than one time or only once is enough.



2000000




I am using Liferay 6.1 when I tried to use Blob data type it is giving me error that tablenamecolumnModelImpl.java not created or something like that this is the link of page of my query
https://www.liferay.com/community/forums/-/message_boards/message/33794040
thank you for such contributions and helps towards other person.
asifaftab87@gmail.com

Post a Comment

Share & Enjoy

Twitter Delicious Facebook Digg Stumbleupon Favorites More