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
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:
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:
- file may have exceeded the upload limit (100MB by default, depends on configuration)
- 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.getSize(paramName) returns 0).
- so at least inform the user about the limit (see method
- file may have been stored in memory only (when its < 1kB = < 1024 bytes)
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
Hope this helps.