public void onModuleLoad() {
final FormPanel form = new FormPanel();
form.setAction("/upload");
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setMethod(FormPanel.METHOD_POST);
VerticalPanel panel = new VerticalPanel();
form.setWidget(panel);
// GWT 提供的 FileUpload 控件
final FileUpload upload = new FileUpload();
upload.setName("uploadFormElement");
panel.add(upload);
// Add a 'submit' button.
panel.add(new Button("Submit", new ClickListener() {
public void onClick(Widget sender) {
form.submit();
}
}));
// Add an event handler to the form.
form.addFormHandler(new FormHandler() {
public void onSubmit(FormSubmitEvent event) {
System.out.println(" ==== onSubmit ====");
// upload.getFilename() 可以得到用户选择的文件路径和文件名
if (upload.getFilename().length() == 0) {
Window.alert("error!");
event.setCancelled(true);
} else if (!upload.getFilename().endsWith(".zip")) {
Window.alert("error.");
event.setCancelled(true);
}
}
public void onSubmitComplete(FormSubmitCompleteEvent event) {
System.out.println(" ==== onSubmitComplete ====");
System.out.println(event.getResults());
}
});
RootPanel.get().add(form);
}
}
标签:文件,form,void,upload,Gwt,FormPanel,new,event
From: https://blog.51cto.com/u_16119425/6307437