Multiple files upload in Liferay 6.1

      No Comments on Multiple files upload in Liferay 6.1

Today I have found a weird bug in Liferay’s UploadServletRequestImpl in the following method

public String getFullFileName(String name) {
FileItem[] liferayFileItems = _fileParams.get(name);

if ((liferayFileItems != null) && (liferayFileItems.length > 0)) {
FileItem liferayFileItem = liferayFileItems[0];

return liferayFileItem.getFullFileName();
}

return null;
}

 

The intention of the above code is to return the full name of the file(s) being uploaded. But actually the above code returns the name of the first file uploaded on the form with the name field.

But what if I have multiple files uploads on my form with the same name like this

<portlet:actionURL var="uploadFileUrl" name="uploadFileAction" />

<form action="${uploadFileUrl}" method="POST" enctype="multipart/form-data">
<input name="supportingDoc" type="file"/>
<input name="supportingDoc" type="file"/>

<button type="submit" class="aca-bt finish-bt" value="Submit">Submit</button>
</form>

 

If you upload two files on the above form the following code will always print name of the first file

UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(request);
System.out.println(uploadPortletRequest.getFullFileName("supportingDoc"));

 

Workaround

You can use uploadPortletRequest.getMultipartParameterMap() to get all the multipart parameters for a parameter name. The following code will print the name of all the files

 

UploadPortletRequest uploadPortletRequest = PortalUtil.getUploadPortletRequest(request);

//Following code will always print the name of the first file uploaded
System.out.println(uploadPortletRequest.getFullFileName("supportingDoc"));

//Following code will iterate through all the files and print their names
for(FileItem fileItem:uploadPortletRequest.getMultipartParameterMap().get("supportingDoc")){
System.out.println(fileItem.getFullFileName());
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *