By Rodrigo Cavalcante

2012-01-16 04:37:59 8 Comments

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:

    <p:fileUpload fileUploadListener="#{fileUploadController.handleFileUpload}"

    <p:growl id="messages" showDetail="true"/>

And the bean:

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.


@BalusC 2012-01-16 12:30:53

How to configure and troubleshoot <p:fileUpload> depends on PrimeFaces version.

All PrimeFaces versions

The below requirements apply to all PrimeFaces versions:

  1. The enctype attribute of the <h:form> needs to be set to multipart/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.

  2. 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 for mode="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.

  3. When using mode="simple" (i.e. non-ajax upload), then ajax must be disabled on any PrimeFaces command buttons/links by ajax="false", and you must use <p:fileUpload value> with <p:commandButton action> instead of <p:fileUpload fileUploadListener> (for PrimeFaces <= 7.x) or <p:fileUpload listener> (for PrimeFaces >= 8.x)

So, if you want (auto) file upload with ajax support (mind the <h:head>!):

<h:form enctype="multipart/form-data">
    <p:fileUpload fileUploadListener="#{bean.upload}" auto="true" /> // for PrimeFaces >= 8.x this should be listener instead of fileUploadListener 
public void upload(FileUploadEvent event) {
    UploadedFile uploadedFile = event.getFile();
    String fileName = uploadedFile.getFileName();
    String contentType = uploadedFile.getContentType();
    byte[] contents = uploadedFile.getContents(); // Or getInputStream()
    // ... Save it, now!

Or if you want non-ajax file upload:

<h:form enctype="multipart/form-data">
    <p:fileUpload mode="simple" value="#{bean.uploadedFile}" />
    <p:commandButton value="Upload" action="#{bean.upload}" ajax="false" />
private UploadedFile uploadedFile; // +getter+setter

public void upload() {
    String fileName = uploadedFile.getFileName();
    String contentType = uploadedFile.getContentType();
    byte[] contents = uploadedFile.getContents(); // Or getInputStream()
    // ... Save it, now!

Do note that ajax-related attributes such as auto, allowTypes, update, onstart, oncomplete, etc are ignored in mode="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 on the temporary file.

PrimeFaces 8.x

Configuration is identical to the 5.x version info below, but if your listener is not called, check if the attriubute is called listener and not (like with pre 8.x versions) fileUploadListener

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 that FacesServlet can continue working as usual):


The <servlet-name> value of facesServlet must match exactly the value in the <servlet> entry of the javax.faces.webapp.FacesServlet in the same web.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 return null when native API is used instead of Apache Commons FileUpload. You need to use UploadedFile#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:

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:

    <param-value>commons</param-value><!-- Allowed values: auto, native and commons. -->


In case it still doesn't work, here are another possible causes unrelated to PrimeFaces configuration:

  1. 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. calling getParameter(), 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 in web.xml. If the request is not a multipart/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 WAR

  2. 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 performed a RequestDispatcher#forward() call. Usually, URL rewrite filters such as PrettyFaces do this. This triggers the FORWARD dispatcher, but filters listen by default on REQUEST 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:

  3. 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's enctype. 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.

@BalusC 2012-01-16 15:17:57

Another cause could be that you didn't register the PrimeFaces upload filter in web.xml as per the PrimeFaces Users Guide. Did you read it anyway? That would however not explain why mode="simple" works for you.

@Rodrigo Cavalcante 2012-01-16 16:11:40

Yes I did read it and I've registered the filter, but I just noticed that it's giving me an error when starting the server "SEVERE: WebModule[/EventsCalendary]PWC1270: Exception starting filter PrimeFaces FileUpload Filter" I feel so dumb for not noticing it before. Any tips on solving this error?

@BalusC 2012-01-16 16:17:41

Perhaps the filter mapping is incorrect. It has to be mapped on the <servlet-name> of the FacesServlet as you've definied in web.xml. It defaults by most IDEs/code-generators to Faces Servlet, but it can be as good facesServlet or something which is more confirm naming conventions.

@Rodrigo Cavalcante 2012-01-16 16:22:51

Nevermind solved it by adding commons-fileupload to the classpath and commons-io, is there anywhere saying that these libraries are needed? Anyway, everything seems to be working right now, the method is being called like it was supposed to, thanks.

@BalusC 2012-01-16 16:28:15

This contradicts with your statement that it works with mode="simple".

@Rodrigo Cavalcante 2012-01-16 16:37:15

But it was working, the method was being called, i don't know if the file was actually being uploaded, but the method was at least being called.

@BalusC 2012-01-16 17:00:33

Okay, but I won't be surprised if the file is empty. I've at least updated the answer.

@sys_debug 2012-03-18 11:02:14

Hello @BalusC I tried many examples of fileupload but all failed. The only thing is dont know where to add the XML bit (filters and mappings) in the web.xml. I mean under which section? because each time I insert the XML bits, the project is flagged red in netbeans. Any hint therE?

@Grégoire C 2015-03-17 09:53:52

The answer should be updated for people who are using PF 4+, Servlet 3.0+, JSF 2.2+. Ok, we don't need to use Apache Commons File Upload anymore, but when using Pretty Faces as a Filter the p:fileUpload won't work out of the box with Tomcat 7. A reference to this answer should be added:…

@Dmitry Alexandrov 2015-11-16 11:03:47

Hello @BalusC, unfortunately any of the configurations works on JBoss 6.4.0.GA when installed JSF 2.2 as a module. Ive tried it with filter.. without filter.. still not working.

@BalusC 2015-11-16 11:18:10

@Dmitry: post results of troubleshooting as suggested in "If you're still having problems".

@Dmitry Alexandrov 2015-11-16 11:40:20

@BalusC actually it worked..... if I specify explicidly filter and commons upload it works, but it stops working on TomEE...

@Dmitry Alexandrov 2015-11-17 11:26:52

So it all came up with the situation that I ant the same config in TomEE and JBoss..

@Pixelstix 2016-09-07 16:24:54

For me, FileUploadRenderer#decode() was bailing right away, due to the content type in org.apache.myfaces.webapp.filter.MultipartRequestWrapper returning WWW_FORM_URLENCODED_TYPE which is "application/x-www-form-urlencoded" - doesn't start with "multipart/". Manually adding the Primefaces filter before other filters didn't work. I removed the org.apache.myfaces.webapp.filter.ExtensionsFilter from the web.xml and then PrimeFaces <p:fileUpload> worked. I'm going to totally remove Tomahawk from my application since it's not being updated and its features are available elsewhere.

@pavlee 2018-05-30 12:11:17

@BalusC In case of non ajax file upload with mode="simple", is there a workaround to use update?

@javshak 2018-08-08 14:50:25

I had the same issue - except that I was working with Primeface 6.1 and Webshpere AS 8.5.5 which does not support JSF 2.2. The change I made were similar to the one mentioned for version 5.x.

@AngelAvila 2018-10-26 19:31:55

I could upload files when my app was running on Payara I migrate my app to Payara 5.183 but even with non-ajax simple mode the UploadedFile object always is null, the file never is uploaded, any ideas?

@Tobias Rath 2018-12-18 16:36:19

Be careful, Primefaces 5.3 can't check higher Versions than JSF 2.2. So in JSF 2.3 the upload mode switch to commons if nothing is declared. Thanks for the tip with the breakpoint.

@Alex D 2019-10-23 13:35:45

With JBoss 7.2(Undertow) and PrimeFaces 6.0 org.primefaces.webapp.filter.FileUploadFilter should be removed from web.xml and context param file uploader should be set to native:


@Kukeltje 2019-10-23 15:48:44

Should? Do you get specific errors if you don't?

@Alex D 2019-10-25 09:18:48

Yes, my FileUploadEvent doesn't invoke without this changes.

@Kukeltje 2019-10-25 17:03:11

That is not an explicit error, that is unexpected behaviour

@Xavier Lambros 2019-02-13 11:52:24

For people using Tomee or Tomcat and can't get it working, try to create context.xml in META-INF and add allowCasualMultipartParsing="true"

<?xml version="1.0" encoding="UTF-8"?>
<Context allowCasualMultipartParsing="true">
  <!-- empty or not depending your project -->

@BalusC 2019-02-13 13:30:08

This is a work around for a wrong filter configuration/ordering.

@Xavier Lambros 2019-02-13 14:09:50

Hi @BalusC, can you give us more explanation? Is there a better way than this work around?

@BalusC 2019-02-13 16:36:05

See my answer in this question.

@Christian Altamirano Ayala 2017-10-05 20:21:35

I had the same issue, due to the fact that I had all the configuration that describe in this post, but in my case was because I had two jquery imports (one of them was primefaces's query) which caused conflicts to upload files.

See Primefaces Jquery conflict

@Kukeltje 2017-10-05 20:30:21

Didn't you get a specific error in the browser developer console then?

@Christian Altamirano Ayala 2017-10-06 04:31:43

@Kukeltje this is what the console showed: Uncaught TypeError: Object [object Object] has no method 'fileupload'

@engilyin 2017-07-28 12:17:09

Neither of the suggestions here were helpful for me. So I had to debug primefaces and found the reason of the problem was:

java.lang.IllegalStateException: No multipart config for servlet fileUpload

Then I have added section into my faces servlet in the web.xml. So that has fixed the problem:



@Waldeyr Mendes da Silva 2017-05-16 12:29:10


    <h:form enctype="multipart/form-data">    
<p:outputLabel value="Choose your file" for="submissionFile" />
                <p:fileUpload id="submissionFile"
                    fileUploadListener="#{bean.uploadFile}" mode="advanced"
                    auto="true" dragDropSupport="false" update="messages"
                    sizeLimit="100000" fileLimit="1" allowTypes="/(\.|\/)(pdf)$/" />



@ViewScoped public class Submission implements Serializable {

private UploadedFile file;


public void uploadFasta(FileUploadEvent event) throws FileNotFoundException, IOException, InterruptedException {

    String content = IOUtils.toString(event.getFile().getInputstream(), "UTF-8");

    String filePath = PATH + "resources/submissions/" + nameOfMyFile + ".pdf";

    MyFileWriter.writeFile(filePath, content);

    FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
            event.getFile().getFileName() + " is uploaded.", null);
    FacesContext.getCurrentInstance().addMessage(null, message);




    <servlet-name>Faces Servlet</servlet-name>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>

@Kukeltje 2017-05-16 12:35:25

Can you explain why this is an answer? It is only code, not explanation or whatever

@romsky 2018-04-11 11:50:54

"#{bean.uploadFile}" vs "#{bean.uploadFasta}" , remove update="messages" and it will (only) that works for me !

@eric A 2017-01-05 13:20:18

I had same issue with primefaces 5.3 and I went through all the points described by BalusC with no result. I followed his advice of debugging FileUploadRenderer#decode() and I discovered that my web.xml was unproperly set


The param-value must be 1 of these 3 values but not all of them!! The whole context-param section can be removed and the default will be auto

@HazeHorizon 2014-02-28 16:46:43

Looks like javax.faces.SEPARATOR_CHAR must not be equal to _

@Karl Richter 2014-12-11 12:29:16

Could you elaborate, please?!

@user1791617 2012-11-01 14:29:33

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.

    <p:fileUpload fileUploadListener="#{fileUploadController.handleFileUpload(event)}"

    <p:growl id="messages" showDetail="true"/>

@Reinaldo Gil 2012-01-31 20:06:17

You are using prettyfaces too? Then set dispatcher to FORWARD:

   <filter-name>PrimeFaces FileUpload Filter</filter-name>
   <servlet-name>Faces Servlet</servlet-name>

@Babl 2016-04-14 15:07:13

This is still an issue when used with OCP Rewrite. I owe you a beer :)

Related Questions

Sponsored Content

12 Answered Questions

1 Answered Questions

[SOLVED] PrimeFaces p:fileUpload does not invoke method

Sponsored Content