0

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Sean
  • 1,416
  • 19
  • 51

1 Answers1

1

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?

Community
  • 1
  • 1
Zaw Than oo
  • 9,651
  • 13
  • 83
  • 131
  • Thanks. However does have the effect that I can't handle normal form fields and file uploading on the same form. – Sean Aug 25 '14 at 12:04