title: 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
body: <p><strong>Important :</strong> The issue which is discussed in this thread has been <a href="https://code.google.com/p/primefaces/issues/detail?id=6925"><strong>fixed</strong></a> as of <strong><a href="http://central.maven.org/maven2/org/primefaces/primefaces/5.1/">PrimeFaces 5.1 final</a></strong> (community release) <a href="http://blog.primefaces.org/?p=3302"><strong>released</strong></a> on Monday, October 6, 2014 (just a few minutes ago from now). I attempted on <a href="http://repo1.maven.org/maven2/org/glassfish/javax.faces/2.2.8-02/"><strong>JSF 2.2.8-02</strong></a> (or <a href="https://maven.java.net/content/repositories/releases/com/sun/faces/jsf-api/2.2.8-02/"><strong>api</strong></a>, <a href="https://maven.java.net/content/repositories/releases/com/sun/faces/jsf-impl/2.2.8-02/"><strong>impl</strong></a>).</p>
  
  <p><strong>As such, if you happened to use that version (<s>or higher, no need to mention</s>), you would not even need to read this question anymore.</strong></p>
  
  <hr>
  
  <p>I have a web application running on</p>
  
  <ul>
  <li>GlassFish 4.0</li>
  <li>Mojarra 2.2.4</li>
  <li>PrimeFaces 4.0 final</li>
  </ul>
  
  <p>Everything except file upload with AJAX works well. The following xhtml file sends multipart contents via an AJAX request triggered by a PrimeFaces command button.</p>
  
  <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:p="http://primefaces.org/ui"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core"&gt;
      &lt;h:head&gt;
          &lt;title&gt;Test&lt;/title&gt;
      &lt;/h:head&gt;
      &lt;h:body&gt;
          &lt;h:form prependId="true" enctype="multipart/form-data"&gt;
              &lt;p:fileUpload id="txtCatImage"
                            value="#{testManagedBean.uploadedFile}"
                            mode="advanced"
                            dragDropSupport="true"
                            fileLimit="1"
                            sizeLimit="100000"
                            multiple="false"
                            allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
                            fileUploadListener="#{testManagedBean.fileUploadListener}"/&gt;
  
              &lt;p:message for="txtCatImage" showSummary="false"/&gt;
              &lt;p:commandButton id="btnSubmit" 
                               actionListener="#{testManagedBean.insert}" 
                               ajax="true" icon="ui-icon-check" value="Save"/&gt;                
          &lt;/h:form&gt;
      &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>The test managed bean:</p>
  
  <pre><code>@ManagedBean
  @ViewScoped
  public final class TestManagedBean implements Serializable {
  
      private static final long serialVersionUID = 1L;
      private UploadedFile uploadedFile;
  
      public TestManagedBean(){}
  
      public UploadedFile getUploadedFile() {
          return uploadedFile;
      }
  
      public void setUploadedFile(UploadedFile uploadedFile) {
          this.uploadedFile = uploadedFile;
      }
  
      public void fileUploadListener(FileUploadEvent event){
          uploadedFile=event.getFile();
      }
  
      public void insert(){
          if(uploadedFile!=null){
              System.out.println(uploadedFile.getFileName());
          }
          else{
              System.out.println("The file object is null.");
          }
      }
  }
  </code></pre>
  
  <p>When a file is uploaded from a file browser, it shows the file name in its listener - <code>fileUploadListener()</code>.</p>
  
  <p>After uploading a file, when the given command button is pressed (<code>ajax="true"</code>), it causes the following exception to be thrown.</p>
  
  <pre><code>WARNING:   javax.servlet.ServletException: The request content-type is not a multipart/form-data
  javax.faces.FacesException: javax.servlet.ServletException: The request content-type is not a multipart/form-data
      at org.primefaces.component.fileupload.NativeFileUploadDecoder.decode(NativeFileUploadDecoder.java:44)
      at org.primefaces.component.fileupload.FileUploadRenderer.decode(FileUploadRenderer.java:44)
      at javax.faces.component.UIComponentBase.decode(UIComponentBase.java:831)
      at javax.faces.component.UIInput.decode(UIInput.java:771)
      at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1225)
      at javax.faces.component.UIInput.processDecodes(UIInput.java:676)
      at javax.faces.component.UIForm.processDecodes(UIForm.java:225)
      at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1220)
      at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1220)
      at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:929)
      at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78)
      at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
      at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
      at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
      at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:70)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
      at filter.NoCacheFilter.doFilter(NoCacheFilter.java:28)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
      at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
      at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
      at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
      at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
      at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
      at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
      at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
      at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
      at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
      at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
      at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
      at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
      at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
      at java.lang.Thread.run(Thread.java:722)
  Caused by: javax.servlet.ServletException: The request content-type is not a multipart/form-data
      at org.apache.catalina.fileupload.Multipart.getPart(Multipart.java:187)
      at org.apache.catalina.connector.Request.getPart(Request.java:4535)
      at org.apache.catalina.connector.RequestFacade.getPart(RequestFacade.java:1095)
      at org.primefaces.component.fileupload.NativeFileUploadDecoder.decodeAdvanced(NativeFileUploadDecoder.java:60)
      at org.primefaces.component.fileupload.NativeFileUploadDecoder.decode(NativeFileUploadDecoder.java:37)
      ... 48 more
  
  SEVERE:   javax.servlet.ServletException: The request content-type is not a multipart/form-data
      at org.apache.catalina.fileupload.Multipart.getPart(Multipart.java:187)
      at org.apache.catalina.connector.Request.getPart(Request.java:4535)
      at org.apache.catalina.connector.RequestFacade.getPart(RequestFacade.java:1095)
      at org.primefaces.component.fileupload.NativeFileUploadDecoder.decodeAdvanced(NativeFileUploadDecoder.java:60)
      at org.primefaces.component.fileupload.NativeFileUploadDecoder.decode(NativeFileUploadDecoder.java:37)
      at org.primefaces.component.fileupload.FileUploadRenderer.decode(FileUploadRenderer.java:44)
      at javax.faces.component.UIComponentBase.decode(UIComponentBase.java:831)
      at javax.faces.component.UIInput.decode(UIInput.java:771)
      at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1225)
      at javax.faces.component.UIInput.processDecodes(UIInput.java:676)
      at javax.faces.component.UIForm.processDecodes(UIForm.java:225)
      at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1220)
      at javax.faces.component.UIComponentBase.processDecodes(UIComponentBase.java:1220)
      at javax.faces.component.UIViewRoot.processDecodes(UIViewRoot.java:929)
      at com.sun.faces.lifecycle.ApplyRequestValuesPhase.execute(ApplyRequestValuesPhase.java:78)
      at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
      at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
      at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:344)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
      at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:70)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
      at filter.NoCacheFilter.doFilter(NoCacheFilter.java:28)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:316)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
      at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
      at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
      at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
      at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
      at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
      at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
      at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
      at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
      at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
      at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
      at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
      at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
      at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
      at java.lang.Thread.run(Thread.java:722)
  </code></pre>
  
  <p>It can only work, when the ajax attribute of the command button is set to false - <code>ajax="false"</code>.</p>
  
  <hr>
  
  <p>I have downgraded the Mojarra version to 2.1.9 in Tomcat 7.0.35. It worked with this Mojarra version along with PrimeFaces 4.0 final (and PrimeFaces 4.0 RC1 too) - files uploaded with an AJAX request.</p>
  
  <p>I have alternatively tried on the following Mojarra versions</p>
  
  <ul>
  <li>2.2.0</li>
  <li>2.2.1</li>
  <li>2.2.2</li>
  <li>2.2.3</li>
  <li>2.2.4</li>
  </ul>
  
  <p>in GlassFish 4.0 but none of them succeeded to upload files with an AJAX request which is highly required because editing of rows using <code>&lt;p:rowEditor/&gt;</code> (along with images in each row, for example) in PrimeFaces DataTable is always AJAX based.</p>
  
  <p>I want to keep GlassFish <strong>4.0</strong> anyhow. I have also tried to downgrade Mojarra 2.1.9 in GlassFish 4.0 but it failed to create bundles ending with an exception. GlassFish 4.0 doesn't seem to work with Mojarra lower than 2.2.x.</p>
  
  <p>So, what is responsible for causing this exception - PrimeFaces or JSF? Just confused. Is there any workaround to upload files with AJAX requests in this given environment?</p>
  
  <hr>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Filter mapping in <code>web.xml</code>:</p>
  
  <pre><code>&lt;filter&gt;
      &lt;filter-name&gt;PrimeFaces FileUpload Filter&lt;/filter-name&gt;
      &lt;filter-class&gt;org.primefaces.webapp.filter.FileUploadFilter&lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;PrimeFaces FileUpload Filter&lt;/filter-name&gt;
      &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
  &lt;/filter-mapping&gt;
  
  &lt;servlet&gt;
      &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;javax.faces.webapp.FacesServlet&lt;/servlet-class&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;Faces Servlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;*.jsf&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <hr>
  
  <p>The problem remains stationary along with the Mojarra version <a href="http://repo1.maven.org/maven2/org/glassfish/javax.faces/2.2.5/"><strong>2.2.5</strong></a> (or <a href="https://maven.java.net/content/repositories/releases/com/sun/faces/jsf-api/2.2.5/">api</a>, <a href="https://maven.java.net/content/repositories/releases/com/sun/faces/jsf-impl/2.2.5/">impl</a>) <a href="https://javaserverfaces.java.net/2.2/download.html">released</a> on January 08, 2014.</p>
  
  <hr>
  
  <p>Once again tried on the Mojarra version <strong><a href="http://repo1.maven.org/maven2/org/glassfish/javax.faces/2.2.6/">2.2.6</a></strong> (or <a href="https://maven.java.net/content/repositories/releases/com/sun/faces/jsf-api/2.2.6/">api</a>, <a href="https://maven.java.net/content/repositories/releases/com/sun/faces/jsf-impl/2.2.6/">impl</a>) <a href="https://javaserverfaces.java.net/2.2/download.html">released</a> on March 04, 2014. The problem remains intact. </p>
  
  <hr>
  
  <p>Still does not work on <a href="http://central.maven.org/maven2/org/primefaces/primefaces/5.0/">PrimeFaces <strong>5.0</strong></a> final <a href="http://blog.primefaces.org/?p=3132">released</a> on May 05, 2014.</p>