开发者

Filters and handlers for FileUpload

开发者 https://www.devze.com 2023-03-23 07:03 出处:网络
In my GWT project I would like to: Set a filter for the FileUpload widget so that it only accepts JPG files.

In my GWT project I would like to:

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

  2. Enable myButton if the FileUpload widget, called chooser, has any file choosen. And disable myButton otherwise.

This is my code for point 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)
    myBu开发者_JS百科tton.setEnabled(false);
else if(chooser.isAttached()==true && myButton.isEnabled()==false)
    myButton.setEnabled(true);
} });


I included a line like the one below:

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

It did work for me using gwt FileUpload


@Point 1: i think, is not possible to filter, which files can be choosed. The only one way for me is 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: the chooser.isAttached() is wrong condition imho....you need to check, if the input field is empty.

0

精彩评论

暂无评论...
验证码 换一张
取 消