UploadPortletRequest and file size limits

Uploading files into portlet is quite common request these days and Liferay offers several ways how to achieve it. The simples way is to use plain HTML form upload and retrieve files on the server side using UploadPortletRequest. It does not need any JavaScript (if you don't want to), one however has to keep in mind that this approach has some limitations. One of these is the size of the file, which can result into the file (its binary content) not being available on the server side.

I'll try to highlight the hurdles we had to get over while implementing this approach on recent project. All examples are based on Liferay EE 6.1.10 ga1.

First part you need to write is the HTML form inside the JSP of your portlet. It shoudl look like following example:

Important is to make sure you:

  • set method="POST" and
  • enctype="multipart/form-data".

Otherwise your file's content will never be sent in the HTTP request body to the server.  Then your portlet code (action method) should look like:

Important parts related to file size:

  1. file may have exceeded the upload limit (100MB by default, depends on configuration)
    • see portal.properties -> com.liferay.portal.upload.UploadServletRequestImpl.max.size
    • there is no easy way how to get the size of the file (when it was too big), it seems to be simply ignored completely (uploadPortletRequest.getFile(paramName) returns null, uploadPortletRequest.getSize(paramName) returns 0).
    • so at least inform the user about the limit (see method validateFileParamNotEmpty())
  2. file may have been stored in memory only (when its < 1kB = < 1024 bytes)
    • then request.getFile(paramName) or request.getFile(paramName, false) would return file handle, but the file is not existing on the disk (was never written into given path), so you cannot read the binary data from it
    • either use request.getFile(paramName, true) or request.getFileAsStream(paramName)

Hope this helps.

Blogs
We had the same problem with small sized files, and your post helped resolve the issue. Thanks for sharing.