3

I am using p:fileUpload but the fileUploadListener is not getting invoked if I use mode="simple" . Is there any way to use fileUploadListener in simple mode.

<p:fileUpload id ="uploading"
              fileUploadListener="#{workflowActionTemplate.handleFileUpload}"
              mode="simple" 
              update="messages"
              sizeLimit="100000" 
              allowTypes="/(\.|\/)(gif|jpe?g|png|pdf)$/"
              multiple="true"/>

Prime faces : 3.2

I have done following configuration, please let me know if I am missing anything.

web.xml:

<!-- File Upload filter -->
<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>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>

pom.xml:

<!-- Dependancy for file upload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2</version>
</dependency>

<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>3.2</version>
</dependency>

listener Method:

public void handleFileUpload(FileUploadEvent event) {
  FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded."); System.out.println("msg : "+ msg);
  uploadedFile = event.getFile();
}

--

If I use value attribute instead of fileUploadListener and if I don't upload the file, then the fileUpload attribute not getting set and so it gives following error.

javax.faces.component.UpdateModelException: javax.el.ELException: /search/workflowAction.xhtml @181,104 value="#{workflowActionTemplate.uploadedFile}": Can't set property 'uploadedFile' of type 'org.primefaces.model.UploadedFile' on class 'com.principal.nq.statements.search.WorkflowActionTemplate$$EnhancerByCGLIB$$6ebcb7eb' to value ''

Update

As fileUploadListener is not working I was also trying to use ajax call in the following way to update the file value. But f:ajax is not able to execute Primefaces p:fileUpload component. I also tried with p:ajax but that is also not working.

<p:fileUpload id="uploading"
              value="#{workflowActionTemplate.uploadedFile}"
              mode="simple"
              update="messages"
              sizeLimit="100000" 
              allowTypes="/(\.|\/)(gif|jpe?g|png|pdf)$/"
              auto="true"/>
<p:growl id="messages" showDetail="true"/>
<h:commandButton id="uploadDocument" styleClass="continuebutton" value="#{msg.upload}" action="#{workflowActionTemplate.uploadParticipantCustomDoc}">
  <f:ajax execute="uploading" render="uploadDocumentDlg" onevent="onAjaxUploadCustomDoc"/>
</h:commandButton>
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Arpit
  • 991
  • 14
  • 17
  • 2
    @RongNK: OP implies that it works fine when `mode="advanced"` is used. @Arpit: Are you nesting forms? – BalusC May 02 '13 at 18:22
  • @BalusC you are right, its not working with simple mode. No I am not nesting the forms. I also tried to use ajax so that I can achieve same functionality but f:ajax & p:ajax both not able to execute primeface p:fileUpload component. – Arpit May 03 '13 at 07:07
  • 1
    The `ELException` indicates that the setter method is missing or wrong. Note that `mode="simple"` don't support ajax (and also that all other attributes related to `mode="advanced"` like `update`, `sizeLimit`, `allowTypes` and `auto` are completely ignored). So if you were trying to upload via ajax all the time while using `mode="simple"`, then you was being wrong with that in first place. See also the link mentioned by RongNK. – BalusC May 03 '13 at 10:37
  • @BalusC Thanks, but I haven't got the solution why fileUploadListener don't work in simple mode. I have searched a lot also followed the steps mentioned in the link provided by RongNK but no luck. – Arpit May 06 '13 at 10:39

1 Answers1

1

Please follow these steps to get your code perfectly fine. in the XHTML file.

    <p:fileUpload id="choose" validator="#{controllerClass.validateFile}" multiple="false" allowTypes="/(\.|\/)(gif|jpe?g|png)$/"  value="#{controllerclass.uploadedfile}" required="true" mode="simple"/>

<p:commandButton ajax="false" id="saveBtn" update="errBrand,pnl" value="Save Brand" action="#{controllerClass.uploadFile()}" />

in the web.xml define following filter and servlet.

  <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>

in the controller class you need to define method body like this

use import org.primefaces.model.UploadedFile;

private UploadedFile uploadedfile;

if you want to define validation method for uploaded file you can write it like this

public void validateFile(FacesContext ctx,
            UIComponent comp,
            Object value) {
        List<FacesMessage> msgs = new ArrayList<FacesMessage>();
        UploadedFile file = (UploadedFile)value;
        int fileByte = file.getContents().length;
        if(fileByte > 15360){
            msgs.add(new FacesMessage("Too big must be at most 15KB"));
        }
        if (!(file.getContentType().startsWith("image"))) {
            msgs.add(new FacesMessage("not an Image file"));
        }
        if (!msgs.isEmpty()) {
            throw new ValidatorException(msgs);
        }
    }
Umair
  • 860
  • 2
  • 13
  • 30
  • Thanks for answering, but I am not sure I would be able to test that out as I am no more working on that area now. Anyway, I appreciate your help. – Arpit Apr 12 '15 at 08:13