6/24/2012

Uploading files with Liferay

This is a short tutorial for those of you who want to upload a file using Liferay portlets. First you need to give your form in your JSP the correct encoding type:
<form name="<portlet:namespace/>fm" method="post" enctype="multipart/form-data" action="<portlet:actionURL />">

This makes sure that the data you upload is really uploaded. Next you should add the upload button like this :

<input type="file" name="<portlet:namespace/>yourfile" id="<portlet:namespace/>yourfile" />


That´s all in the JSP. If you now add the following in your processAction method, you have access to your uploaded file:

public void processAction(ActionRequest request, ActionResponse response)throws PortletException, IOException {


UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(request);

String sourceFileName = uploadRequest.getFileName("yourfile");
File file = uploadRequest.getFile("yourfile");


That´s all. Have fun :)



If you like this tutorial it would be very nice, if you could click on some of the google ads you see on the right side. It helps me run this block and motivates me ;)

If you have any questions, feel free to leave a comment.

6/01/2012

How to get the JournalStructure data

If you create a WebContent that is based on a structure / template combination and you want to get the value of one of the structure fields programmatically, you can do it like this:


Imagine, that your WebContent has the ID 21623 and you are looking for a field that has the name "Text1".


JournalArticle article = JournalArticleLocalServiceUtil.getArticle(21623);

Document document = SAXReaderUtil.read(article.getContentByLocale(Locale.ENGLISH.toString()));
Node node = document.selectSingleNode("/root/dynamic-element[@name='Text1']/dynamic-content");
String value = node.getText();

That´s all :) 

 If you like this post it would be very nice, if you could click on some of the google ads you see on the right side. It helps me run this block and motivates me ;)

If you have any questions, feel free to leave a comment.