0

The UploadedFile is null when I clicked the commandButton. What's Wrong? in the form tag i insert the code : enctype="multipart/form-data"

 <h:form  enctype="multipart/form-data">
     <p:fileUpload  mode="simple"  value="#{b_cargar_tbl.file}" />
         <p:commandButton actionListener="#{b_cargar_tbl.upload()}"  value="Send" ajax="false" />
 </h:form>

The code of bean is:

private UploadedFile file;

public UploadedFile getFile() {
    return file;
}

public void setFile(UploadedFile file) {
    this.file = file;
}

public void upload() {
    if(file != null) {
        FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, message);
    }
}

The web.xlm

  <context-param>
    <param-name>primefaces.UPLOADER</param-name>
    <param-value>auto|native|commons</param-value>
</context-param>
<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>

1 Answers1

0

this solution: add atribute fileUploadListener into p:fileUpload and create in your managedbean class method with one parametr listener FileUploadEvent

example:

<p:commandButton actionListener="#{b_cargar_tbl.upload()}"  value="Send" 
fileUploadListener="#{b_cargar_tbl.upload}" ajax="false" />

in your managed bean add method:

public void upload(FileUploadEvent event) {
        System.err.println("event.getFile().getFileName() = " + event.getFile().getFileName());     
        try {
            copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }
Piotr Rogowski
  • 3,642
  • 19
  • 24
  • I found the solution: In the web.xml there are 3 values , i configured "auto" and work fine: primefaces.UPLOADER auto|native|commons – Santiago Matiz Sep 02 '16 at 20:58