gwt文件上传代码

时间:2011-06-20 10:45:44

标签: gwt file-upload

我想在我的应用程序中上传文件,并希望设置在我的本地系统上传后应该保存文件的路径。我使用以下代码但是在提交按钮时没有得到响应而点击。请告诉我代码适用于gwt中的文件上传。 [代码]

public class FormPanelExample implements EntryPoint {

  public void onModuleLoad() {
    // Create a FormPanel and point it at a service.
    final FormPanel form = new FormPanel();
    form.setAction("/myFormHandler");

    // Because we're going to add a FileUpload widget, we'll need to set the
    // form to use the POST method, and multipart MIME encoding.
    form.setEncoding(FormPanel.ENCODING_MULTIPART);
    form.setMethod(FormPanel.METHOD_POST);

    // Create a panel to hold all of the form widgets.
    VerticalPanel panel = new VerticalPanel();
    form.setWidget(panel);

    // Create a TextBox, giving it a name so that it will be submitted.
    final TextBox tb = new TextBox();
    tb.setName("textBoxFormElement");
    panel.add(tb);

    // Create a ListBox, giving it a name and some values to be associated with
    // its options.
    ListBox lb = new ListBox();
    lb.setName("listBoxFormElement");
    lb.addItem("foo", "fooValue");
    lb.addItem("bar", "barValue");
    lb.addItem("baz", "bazValue");
    panel.add(lb);

    // Create a FileUpload widget.
    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) {
        // This event is fired just before the form is submitted. We can take
        // this opportunity to perform validation.
        if (tb.getText().length() == 0) {
          Window.alert("The text box must not be empty");
          event.setCancelled(true);
        }
      }

      public void onSubmitComplete(FormSubmitCompleteEvent event) {
        // When the form submission is successfully completed, this event is
        // fired. Assuming the service returned a response of type text/html,
        // we can get the result text here (see the FormPanel documentation for
        // further explanation).
        Window.alert(event.getResults());
      }
    });

    RootPanel.get().add(form);
  }
}

由于

爱玛

3 个答案:

答案 0 :(得分:1)

我终于使用此Link

解决了问题

这正是我所寻找的。

答案 1 :(得分:0)

现在,我还记得..还有在使FormPanel中代码中的错误form.submit()不工作,当窗体的类型从默认的变化(不知道它是否在任何版本未定GWT)。如果您创建一个“本机”提交按钮,如下所示:

HTML nativeSubmitButton new HTML("<input class='gwt-Button' type='submit' value='" + buttonText + "' />")

它将提交表格。

缺点是你不能在这个对象上使用任何Button方法,因为它是一个简单的HTML包装器。所以,禁止在提交按钮(以避免意外双重提交,并给予反馈的形式实际上是提交)将无法正常工作。

我已经创建用于此目的的自己称为DisableableSubmitButton基本上是被一个HTML按钮像上面,以及一个GWT按钮被禁用,并且一些逻辑来切换它们中的每一个可见FlowPanel一个工具类。既然不能修改HTML按钮的实际启用的状态所有提交处理程序必须问这个类,如果它的“启用”与否并取消该事件,如果它是。如果你在这个实现有兴趣,我可以与你分享(我不想洪水与代码计算器,除非你有兴趣)。

答案 2 :(得分:0)

您可能会发现此link有用。虽然它使用图像上传,但您可以将其用于任何类型的文件上传,并进行一些小的更改。