1) Make sure you have configured the extension filter in the web.xml

<!-- Extensions Filter -->
    <filter>
        <filter-name>extensionsFilter</filter-name>
        <filter-class>org.apache.myfaces.component.html.util.ExtensionsFilter</filter-class>
        <init-param>
            <param-name>uploadMaxFileSize</param-name>
            <param-value>100m</param-value>
            <description>Set the size limit for uploaded files.
                Format: 10 - 10 bytes
                        10k - 10 KB
                        10m - 10 MB
                        1g - 1 GB
            </description>
        </init-param>
        <init-param>
            <param-name>uploadThresholdSize</param-name>
            <param-value>100k</param-value>
            <description>Set the threshold size - files
                    below this limit are stored in memory, files above
                    this limit are stored on disk.

                Format: 10 - 10 bytes
                        10k - 10 KB
                        10m - 10 MB
                        1g - 1 GB
            </description>
        </init-param>
        <!--
        <init-param>
            <param-name>uploadRepositoryPath</param-name>
            <param-value>/temp</param-value>
            <description>Set the path where the intermediary files will be stored.
            </description>
        </init-param>
        -->
    </filter>

    <filter-mapping>
        <filter-name>extensionsFilter</filter-name>
        <url-pattern>*.shtml</url-pattern>
    </filter-mapping>

2) Make sure your form is properly encoded and add the t:inputFileUpload

<h:form enctype="multipart/form-data" id="myForm" name="myForm">
...
<t:inputFileUpload 
  id="myUploadedFile" 
  storage="file" accept="image/*" 
  styleClass="myStyle" 
  value="#{myBackingBean.myUploadedFile}"/>
...
</h:form>

3) add UploadedFile instance variable and setter/getter to backing bean

import org.apache.myfaces.custom.fileupload.UploadedFile;
...
public class MyBackingBean {
...
  private UploadedFile myUploadedFile;
...
  public UploadedFile getMyUploadedFile() {
    return myUploadedFile;
  }

  public void setMyUploadedFile(UploadedFile myUploadedFile) {
    this.myUploadedFile = myUploadedFile;
  }
...
}

4) Add code to a form action method that retrievest the contents of UploadedFile for handling

public class MyBackingBean {
  ...
  public String myAction () {
    ...
    ... myUploadedFile.getBytes();
    ... myUploadedFile.getContentType();
    ... myUploadedFile.getName();
    ...
    return "success";
  }
  ...
}

Saving upload despite any validation errors

In many cases you will want to save the uploaded file, even if there are validation errors. Otherwise the user must upload the file again, which is annoying and may be very time-consuming. To do that, don't value-bind the component to a backing bean, but use a ValueChangeListener and set immediate="true".

<t:inputFileUpload storage="file" immediate="true">
        <f:valueChangeListener type="com.myorg.foo.UploadListener" />
</t:inputFileUpload>

public class UploadListener implements ValueChangeListener {
        public void processValueChange(ValueChangeEvent event) throws AbortProcessingException {
                UploadedFile uploadedFile = (UploadedFile) event.getNewValue();
                
                // do something with uploadedFile
        }
}

It's up to you to decide what to do with uploadedFile in your UploadListener. One possibility might be to store the file in a temporary folder, using the session id (from ExternalContext). When the form is finally submitted, persist your data to the database and then move the file to a new final directory using the database ID.

Setup_For_File_Uploads (last edited 2009-09-20 23:01:46 by localhost)