3

I am using myfaces ExtensionFilter to upload a file, but the file is not getting set to my bean for further processing.

Here is the code :

<h:form id="uploadFileForm" enctype="multipart/form-data">
    <tom:inputFileUpload id="file" 
        value="#{paramUpload.uploadFile}">
        <f:valueChangeListener type="com.bosch.de.plcd.plugin.ParamFileUpload" />
    </tom:inputFileUpload>
    <a4j:commandButton value="#{tpMsgs.upload}"
        styleClass="button" action="#{paramUpload.uploadParamFile}"
        onclick="javascript:updateParentScreen();">
    </a4j:commandButton>
</h:form>

and web.xml configuration is as below

<filter>
    <filter-name>Extensions Filter</filter-name>
    <filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>Extensions Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

Since UploadFile was not set to bean, I also tried using ValueChangeListener, never the less, its not se to bean :)

Bean code

public class ParamFileUpload implements ValueChangeListener {

    private UploadedFile uploadFile;

    public void uploadParamFile() {
        if(uploadFile != null) {
            LOGGER.info("File type: " + uploadFile.getContentType());
            LOGGER.info("File name: " + uploadFile.getName());
            LOGGER.info("File size: " + uploadFile.getSize() + " bytes");
        }
    }

    public UploadedFile getUploadFile() {
        return uploadFile;
    }

    public void setUploadFile(UploadedFile uploadFile) {
        this.uploadFile = (UploadedFile)uploadFile;
    }

    public void processValueChange(ValueChangeEvent event)
        throws AbortProcessingException {
        this.uploadFile = (UploadedFile) event.getNewValue();
    }
}

Do you see any other configuration that I might be missing here?

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Satya
  • 2,094
  • 6
  • 37
  • 60
  • have you tried using `rich:fileUpload` tag component from RichFaces? or why don't you want to use it? Also, what versions of JSF, RF and Tomahawk are you using? – Luiggi Mendoza May 25 '12 at 14:35
  • Yes I had tried, but since I also use external JQuery, richfaces fileUpload was not behaving as expected. So had to implement as – Satya May 29 '12 at 04:03

1 Answers1

5

It is not possible to upload files by ajax with current JSF and Tomahawk version. Ajax requests as created by JSF do not support nor use multipart/form-data. Tomahawk is not an ajax based component library. Use a normal command button instead.

<h:commandButton value="#{tpMsgs.upload}"
    styleClass="button" action="#{paramUpload.uploadParamFile}"
    onclick="javascript:updateParentScreen();">
</h:commandButton>

Support for uploading files with ajax is scheduled for upcoming JSF 2.2 with new <h:inputFile> component.

Alternatively, you can use RichFaces' own <rich:fileUpload>. Depending on the RichFaces version used, it uses either Flash or an iframe hack under the covers to achieve asynchronous file uploading.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Richfaces component was not working as expected coz of usage of external jQuery plugin. So had to do this way. But now it works fine :) – Satya May 29 '12 at 04:06