Sean
Sean

Reputation: 1474

Primefaces file uploaded not setting file object in backing bean

I am using the Primefaces 5.0 file uploaded with the Apache Commons Uploader. The control appears on the web page and I can select a file, but when I click a button to action it the file object in the controller has not been set.

The web page:

<h:form id="master">
  <p:panelGrid columns="2">
    <p:fileUpload value="#{controller.file}" mode="simple"/>
    <p:commandButton value="Upload" 
        ajax="false" actionListener="# {controller.uploadFile}" />
  </p:panelGrid>

Controller:

@ManagedBean
@ViewScoped
public class Controller implements Serializable {

private UploadedFile file;

public UploadedFile getFile() {
    return file;
}

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

public void uploadFile() {
    if(file != null) {
        //Doesn't get here because file is null
    }
}

web.xml

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

I tried setting the File Uploader to "auto" in the web.xml, but then I get multi-part exceptions which I couldn't resolve.

Upvotes: 0

Views: 887

Answers (1)

Zaw Than oo
Zaw Than oo

Reputation: 9935

You miss to use enctype="multipart/form-data" in <h:form>. Change as below

<h:form enctype="multipart/form-data">
    ...
</h:form>

What does enctype='multipart/form-data' mean?

Upvotes: 1

Related Questions