9

I am facing a problem with <p:fileUpload> of PrimeFaces. I created a Facelet page to upload a file as below:

<h:form id="welcomeForm">
    <p:fileUpload value="#{fileUploadController.uploadedFile}" mode="simple" />
    <h:commandButton value="Submit" action="#{fileUploadController.submit}" />
    <h:message for="welcomeForm" />
</h:form> 

And a backing bean as below:

public class FileUploadController {   

    private UploadedFile uploadedFile;

    public FileUploadController() {
    }       

    public UploadedFile getUploadedFile() {
        return uploadedFile;
    }

    public void setUploadedFile(UploadedFile uploadedFile) {
        this.uploadedFile = uploadedFile;
    }

    public void submit() {    
        // Get information you from the uploaded file
        System.out.println("Uploaded file name : " + uploadedFile);
    }

} 

When I click the Submit button, the method submit() is called but it the result is as below :

INFO: Uploaded file name : null

How is this caused and how can I solve it?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
user969539
  • 91
  • 1
  • 1
  • 4
  • 3 questions for you: what Primefaces version do you use? why do you use the simple upload? why have you written a public default constructor? – spauny Dec 21 '11 at 11:19
  • I tested the advanced mode but its not work. I used primefaces 3.0 m4 – user969539 Dec 22 '11 at 09:49

5 Answers5

13

Please read the <p:fileUpload> chapter of the PrimeFaces User Guide.

Getting started with FileUpload

First thing to do is to configure the fileupload filter which parses the multipart request. FileUpload filter should map to Faces Servlet.

<filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

Simple File Upload

Simple file upload mode works in legacy mode with a file input whose value should be an UploadedFile instance.

<h:form enctype="multipart/form-data">
    <p:fileUpload value="#{fileBean.file}" mode="simple" />
    <p:commandButton value="Submit" ajax="false"/>
</h:form>

import org.primefaces.model.UploadedFile;

public class FileBean {
    private UploadedFile file;
    //getter-setter
}

Please note the enctype="multipart/form-data" attribute of the form. This is mandatory for HTML in order to be able to send files to the server. The filter is mandatory for JSF in order to extract the data from multipart/form-data requests. Without either of them, either the command action won't be invoked, or all properties will be null.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • thank you for your response but I just want to inform you that I've tested what you posted but its not working. I used primefaces 3.0 m4 and glassfish 3.1.1 – user969539 Dec 22 '11 at 09:54
  • You didn't say which application server you're using, but I believe you're running into the same issue I reported in [How to use multipart/form-data encoding in application running on GlassFish 3.1.1](http://stackoverflow.com/questions/9074615/how-to-use-multipart-form-data-encoding-in-application-running-on-glassfish-3-1). The culprit appears to be an unimplemented feature in GlassFish that will be implemented in version 3.1.2 as indicated [here](http://java.net/jira/browse/GLASSFISH-16740). – Kevin Rahe Jan 31 '12 at 13:59
  • Sorry - you _did_ say that you're running GlassFish 3.1.1. #-| – Kevin Rahe Jan 31 '12 at 14:06
  • 1
    I'm guessing that you need the same solution I did, which is to add the commons-fileupload and commons-io libraries to your project, as described in the answer to [How to use PrimeFaces p:fileUpload? Listener method is never invoked](http://stackoverflow.com/questions/8875818). – Kevin Rahe Jan 31 '12 at 18:10
11

I think that the problem is that the simple upload doesn't support ajax. you should add ajax="false":

<h:form id="welcomeForm">
    <p:fileUpload value="#{fileUploadController.uploadedFile}" mode="simple" />
    <h:commandButton value="Submit" action="#{fileUploadController.submit}" ajax="false" />
    <h:message for="welcomeForm" />
</h:form> 

Or use the primefaces autouploader.

Ben
  • 10,020
  • 21
  • 94
  • 157
  • 1
    I also had to include **commons-fileupload-1.x.x.jar** and **commons-io-2.x.jar** in my WEB-INF/lib – Pakman Oct 17 '12 at 18:44
4

Try This:

<h:form id="welcomeForm" enctype="multipart/form-data">
    <p:fileUpload value="#{fileUploadController.uploadedFile}" mode="simple" />
    <h:commandButton value="Submit" action="#{fileUploadController.submit}" />
    <h:message for="welcomeForm" />
</h:form>

(enctype="multipart/form-data" is very important for file upload.)

And:

public class FileUploadController {   

    private UploadedFile uploadedFile;

    public FileUploadController() {
    }       

    public UploadedFile getUploadedFile() {
        return uploadedFile;
    }

    public void setUploadedFile(UploadedFile uploadedFile) {
        this.uploadedFile = uploadedFile;
    }

    public void submit() {    
        // Get information you from the uploaded file
        System.out.println("Uploaded file name : " + uploadedFile.getFileName());
    }

} 

uploadedFile.getFileName() used to get the file name.

Kijewski
  • 25,517
  • 12
  • 101
  • 143
Praveen
  • 41
  • 1
4

I have the same issue, I have solved it by adding

<filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

like BalusC said.

but adding this :

<!-- JSF mapping -->
<servlet>
   <servlet-name>Faces Servlet</servlet-name>
   <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
</servlet>

<!-- Map these files with JSF -->
<servlet-mapping>
   <servlet-name>Faces Servlet</servlet-name>
   <url-pattern>*.jsf</url-pattern>
</servlet-mapping>

Because by default in J2EE 6 this part is optional JSF 2.0 Servlet activates automatically when the WEB-INF/faces-config.xml descriptor is present.

But it necessary to active correctly PrimeFaces Filter

Jboss 6.1.0.Final / PrimeFaces 3.0.RC2

DMEx38
  • 156
  • 5
0

I had the same issue and just a very little THING saved me:

I did all the internet says about the prime faces and file upload thing

After applying the needed filter mappings and needed dependencies, I saw that dispacher information must also be added to the filter mappings:

<filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>FacesServlet</servlet-name>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

<servlet>
    <servlet-name>FacesServlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>FacesServlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>FacesServlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

Maybe this very little configuration can save lives just like mine, good luck!

Bahadir Tasdemir
  • 10,325
  • 4
  • 49
  • 61