In my GWT project I would like to:
Set a filter for the FileUpload widget so that it only accepts JPG files.
Enable
myButton
if the FileUpload widget, calledchooser
, has any file choosen. And disablemyButton
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.
精彩评论