Filters and handlers for FileUpload

In my GWT project, I would like to:

  • Set the filter for the FileUpload widget so that it accepts only JPG files.

  • Include myButtonif the FileUpload widget called chooserhas any file selected. And unplug myButtonotherwise.

This is my code for item 2, but it does not work. Any ideas? Thanks in advance!

chooser.addAttachHandler(new Handler() {
public void onAttachOrDetach(AttachEvent event) {
if(chooser.isAttached()==false && myButton.isEnabled()==true)
    myButton.setEnabled(false);
else if(chooser.isAttached()==true && myButton.isEnabled()==false)
    myButton.setEnabled(true);
} });
+5
source share
2 answers

@Point 1: I think it’s not possible to filter which files can be selected. The only way for me is to compare in the form handler, for example:

form.addFormHandler(new FormHandler(){
    public void onSubmit(FormSubmitEvent event){
      if(!extension.equals("pdf")) {
         // Error
      } else {
         // Submit
      }
    }
}

Another solution is to use ExtGWT with FileValidator:

fileUpload = new FileUploadField();
fileUpload.setWidth("240");
fileUpload.setValidator(new FileValidator());
fileUpload.setName("file");
fileUpload.setAccept("pdf");

@Point 2: chooser.isAttached() imho.... , .

+2

, :

fileUpload.getElement().setAttribute("accept", "image/png, image/gif,image/jpeg");

gwt FileUpload

+3

All Articles