Multiple File Upload without using SWF in Liferay

This is post is the alternate solution for those who dont want to use Liferay's in built Multiple file up loader.
Liferay's in built multiple file up loader uses SWF (Flash) in background and there some issue with flash and other third party tools.

So we decided not use liferay's DL Multifile uploader instead we developed our own.

Basically I have used jQuery plugin at front end and in java code simply to store file in Document Library.
You can download jQuery plugin from here ...jQuery Multiple FileUpload



@ActionMapping(params = "myActions=uploadMultipleFile")
public void uploadFiles(ActionRequest request, ActionResponse response) throws PortletException
{
_log.info("Inside UploadMultipleFiles()..");
// HttpServletRequest httpRequest =
// PortalUtil.getHttpServletRequest(request);
UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(request);
File tempFile;
Map<string, file=""> fileMap = new LinkedHashMap<string, file="">();
Enumeration<string> paramEnum = uploadRequest.getParameterNames();
while (paramEnum.hasMoreElements())
{
String parameter = paramEnum.nextElement();
if (parameter.startsWith("uploadFile"))
{
tempFile = uploadRequest.getFile(parameter);
tempFile = renameFileName(tempFile, uploadRequest, parameter);
if (tempFile != null)
{
fileMap.put(parameter, tempFile);
}
}
}
for (Map.Entry<string, file=""> entry : fileMap.entrySet())
{
singleFileUpload(request, response, entry.getValue());
}
}
<portlet:actionurl var="uploadFileURL">
<portlet:param name="myActions" value="uploadMultipleFile">
</portlet:param></portlet:actionurl>
<script type="text/javascript">
jQuery(function(){ // wait for document to load
jQuery('#uploadFile').MultiFile({
STRING: {
remove: '<img src="http://www.fyneworks.com/@/bin.gif" height="16" width="16" alt="x"/>'
}
});
});
function uploadNewFile()
{
jQuery('#fileTags').find('input[type="file"]').each(
function() {
var id = jQuery(this).attr('id');
jQuery(this).attr('name',id);
});
document.multipleUpload.action='<portlet:actionURL><portlet:param name="myActions" value="uploadMultipleFile"/></portlet:actionURL>';
document.multipleUpload.submit();
}
</script>
<form enctype="multipart/form-data" method="post" name="multipleUpload">
<table id="fileTags">
<tbody>
<tr><td>Upload File</td>
<td><input class="multi" id="uploadFile" name="uploadFile" type="file" />
</td>
<td></td>
</tr>
</tbody></table>
<input onclick="uploadNewFile()" type="button" value="Upload" />
</form>

Above method will simply identify file inputs from the request. It will iterate each and every file input and store in DL. Even though its not a good approach but any how you can stay out of that flash using this.
public File renameFileName(File sourceFile, UploadPortletRequest uploadRequest, String paramName)
{
File destination = null;
String path = sourceFile.getPath();
path = path.substring(0, path.lastIndexOf(StringPool.BACK_SLASH) + 1);
String fileNm = uploadRequest.getFileName(paramName);
if (Validator.isNotNull(fileNm))
{
path = path.concat(fileNm);
destination = new File(path);
FileUtil.copyFile(sourceFile, destination);
FileUtil.delete(sourceFile);
}
return destination;
}
view raw FileRename.java hosted with ❤ by GitHub

Above method will rename the uploaded file to its actual name as name of the uploaded file is getting changed if you use UploadPortletRequest.
public void singleFileUpload(ActionRequest actionRequest, ActionResponse actionResponse, File file)
{
ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
DLFileEntry fileEntry = null;
String title = "Jit" + StringPool.UNDERLINE + file.getName();
// title= requestId + StringPool.DASH + fileName;
try
{
DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(themeDisplay.getScopeGroupId(), 0, "TestFolder");
ServiceContext serviceContext = ServiceContextFactory.getInstance(DLFileEntry.class.getName(),
actionRequest);
fileEntry = DLFileEntryServiceUtil.addFileEntry(dlFolder.getFolderId(), file.getName(), title,
StringPool.BLANK, null, file, serviceContext);
} catch (Exception e)
{
_log.error("Error while adding file : " + e.getMessage());
e.printStackTrace();
}
}

Above code is to add file into document library.

Next PostNewer Post Previous PostOlder Post Home