Rectangle 27 0

jsf PrimeFaces p:fileUpload Listener method is never invoked for mode="simple"?


<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()}" />
<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>
import org.primefaces.model.UploadedFile;
private UploadedFile uploadedfile;
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);
        }
    }

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

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.

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

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

in the web.xml define following filter and servlet.

Note