I'm trying to upload a file using PrimeFaces, but the fileUploadListener
method isn't being invoked after the upload finishes.
Here is the view:
<h:form>
<p:fileUpload fileUploadListener="#{fileUploadController.handleFileUpload}"
mode="advanced"
update="messages"
sizeLimit="100000"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>
<p:growl id="messages" showDetail="true"/>
</h:form>
And the bean:
@ManagedBean
@RequestScoped
public class FileUploadController {
public void handleFileUpload(FileUploadEvent event) {
FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
I've placed a breakpoint on the method, but it's never called. When using mode="simple"
and ajax="false"
, it is been invoked, but I want it to work in the advanced mode. I'm using Netbeans and Glassfish 3.1.
Looks like javax.faces.SEPARATOR_CHAR must not be equal to _
Neither of the suggestions here were helpful for me. So I had to debug primefaces and found the reason of the problem was:
Then I have added section into my faces servlet in the web.xml. So that has fixed the problem:
You are using prettyfaces too? Then set dispatcher to FORWARD:
How to configure and troubleshoot
<p:fileUpload>
depends on PrimeFaces version.All PrimeFaces versions
The below requirements apply to all PrimeFaces versions:
The
enctype
attribute of the<h:form>
needs to be set tomultipart/form-data
. When this is absent, the ajax upload may just work, but the general browser behavior is unspecified and dependent on form composition and webbrowser make/version. Just always specify it to be on the safe side.When using
mode="advanced"
(i.e. ajax upload, this is the default), then make sure that you've a<h:head>
in the (master) template. This will ensure that the necessary JavaScript files are properly included. This is not required formode="simple"
(non-ajax upload), but this would break look'n'feel and functionality of all other PrimeFaces components, so you don't want to miss that anyway.When using
mode="simple"
(i.e. non-ajax upload), then ajax must be disabled on any PrimeFaces command buttons/links byajax="false"
, and you must use<p:fileUpload value>
with<p:commandButton action>
instead of<p:fileUpload fileUploadListener>
.So, if you want (auto) file upload with ajax support (mind the
<h:head>
!):Or if you want non-ajax file upload:
Do note that ajax-related attributes such as
auto
,allowTypes
,update
,onstart
,oncomplete
, etc are ignored inmode="simple"
. So it's needless to specify them in such case.Also note that you should read the file contents immediately inside the abovementioned methods and not in a different bean method invoked by a later HTTP request. This is because the uploaded file contents is request scoped and thus unavailable in a later/different HTTP request. Any attempt to read it in a later request will most likely end up with
java.io.FileNotFoundException
on the temporary file.PrimeFaces 5.x
This does not require any additional configuration if you're using JSF 2.2 and your
faces-config.xml
is also declared conform JSF 2.2 version. You do not need the PrimeFaces file upload filter at all. In case it's unclear to you how to properly install and configure JSF depending on the target server used, head to How to properly install and configure JSF libraries via Maven? and "Installing JSF" section of our JSF wiki page.If you're however not using JSF 2.2 yet and you can't upgrade it (should be effortless when already on a Servlet 3.0 compatible container), then you need to manually register the below PrimeFaces file upload filter in
web.xml
(it will parse the multi part request and fill the regular request parameter map so thatFacesServlet
can continue working as usual):The
<servlet-name>
value offacesServlet
must match exactly the value in the<servlet>
entry of thejavax.faces.webapp.FacesServlet
in the sameweb.xml
. So if it's e.g.Faces Servlet
, then you need to edit it accordingly to match.PrimeFaces 4.x
The same story as PrimeFaces 5.x applies on 4.x as well.
There's only a potential problem in getting the uploaded file content by
UploadedFile#getContents()
. This will returnnull
when native API is used instead of Apache Commons FileUpload. You need to useUploadedFile#getInputStream()
instead. See also How to insert uploaded image from p:fileUpload as BLOB in MySQL?Another potential problem with native API will manifest is when the upload component is present in a form on which a different "regular" ajax request is fired which does not process the upload component. See also File upload doesn't work with AJAX in PrimeFaces 4.0/JSF 2.2.x - javax.servlet.ServletException: The request content-type is not a multipart/form-data.
Both problems can also be solved by switching to Apache Commons FileUpload. See PrimeFaces 3.x section for detail.
PrimeFaces 3.x
This version does not support JSF 2.2 / Servlet 3.0 native file upload. You need to manually install Apache Commons FileUpload and explicitly register the file upload filter in
web.xml
.You need the following libraries:
commons-fileupload.jar
commons-io.jar
Those must be present in the webapp's runtime classpath. When using Maven, make sure they are at least runtime scoped (default scope of compile is also good). When manually carrying around JARs, make sure they end up in
/WEB-INF/lib
folder.The file upload filter registration detail can be found in PrimeFaces 5.x section here above. In case you're using PrimeFaces 4+ and you'd like to explicitly use Apache Commons FileUpload instead of JSF 2.2 / Servlet 3.0 native file upload, then you need next to the mentioned libraries and filter also the below context param in
web.xml
:Troubleshooting
In case it still doesn't work, here are another possible causes unrelated to PrimeFaces configuration:
Only if you're using the PrimeFaces file upload filter: There's another
Filter
in your webapp which runs before the PrimeFaces file upload filter and has already consumed the request body by e.g. callinggetParameter()
,getParameterMap()
,getReader()
, etcetera. A request body can be parsed only once. When you call one of those methods before the file upload filter does its job, then the file upload filter will get an empty request body.To fix this, you'd need to put the
<filter-mapping>
of the file upload filter before the other filter inweb.xml
. If the request is not amultipart/form-data
request, then the file upload filter will just continue as if nothing happened. If you use filters that are automagically added because they use annotations (e.g. PrettyFaces), you might need to add explicit ordering via web.xml. See How to define servlet filter order of execution using annotations in WAROnly if you're using the PrimeFaces file upload filter: There's another
Filter
in your webapp which runs before the PrimeFaces file upload filter and has performed aRequestDispatcher#forward()
call. Usually, URL rewrite filters such as PrettyFaces do this. This triggers theFORWARD
dispatcher, but filters listen by default onREQUEST
dispatcher only.To fix this, you'd need to either put the PrimeFaces file upload filter before the forwarding filter, or to reconfigure the PrimeFaces file upload filter to listen on
FORWARD
dispatcher too:There's a nested
<h:form>
. This is illegal in HTML and the browser behavior is unspecified. More than often, the browser won't send the expected data on submit. Make sure that you are not nesting<h:form>
. This is completely regardless of the form'senctype
. Just do not nest forms at all.If you're still having problems, well, debug the HTTP traffic. Open the webbrowser's developer toolset (press F12 in Chrome/Firebug23+/IE9+) and check the Net/Network section. If the HTTP part looks fine, then debug the JSF code. Put a breakpoint on
FileUploadRenderer#decode()
and advance from there.Saving uploaded file
After you finally got it to work, your next question shall probably be like "How/where do I save the uploaded file?". Well, continue here: How to save uploaded file in JSF.
bean.xhtml
Bean.java
@ViewScoped public class Submission implements Serializable {
}
web.xml
One point I noticed with Primefaces 3.4 and Netbeans 7.2:
Remove the Netbeans auto-filled parameters for function handleFileUpload i.e. (event) otherwise event could be null.