【问题标题】:gwt file upload codegwt文件上传代码
【发布时间】:2022-01-16 14:02:00
【问题描述】:

我想在我的应用程序中上传文件,并想设置在本地系统中上传后文件应保存的路径。我正在使用以下代码,但在提交按钮上单击时没有响应。请告诉我适用于 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);
  }
}

谢谢

阿曼迪普

【问题讨论】:

  • 文件上传了吗?我自己使用 GWT 上传文件时遇到了一些问题,上传工作正常,但没有触发回调。我会试着找出我做了什么来解决它。 (我确实记得我最终在后端编写了自己的服务句柄)
  • 没有文件没有上传我不是在服务器端调用代码它是一个客户端java程序所以调用代码没有问题。我不知道如何设置上传文件的位置。
  • @stein 我猜回调没有触发的原因是:你可能没有设置formPanel.errorReader。如果您的代码与 amandeep 的相同。

标签: gwt file-upload


【解决方案1】:

现在,我记得.. FormPanel 代码中有一个错误导致form.submit() 不起作用,当表单的类型从默认值更改时(不知道它是否在任何版本的 GWT 中都已修复) )。如果您像这样创建“本机”提交按钮:

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

它将提交表单。

缺点是你不能在这个对象上使用任何 Button 方法,因为它是一个简单的 HTML 包装器。因此禁用提交按钮(以避免意外重复提交,并提供表单实际提交的反馈)将不起作用。

为此,我自己创建了一个名为 DisableableSubmitButton 的实用程序类,它本质上是一个 FlowPanel,具有一个与上述类似的 HTML 按钮,一个禁用的 gwt 按钮,以及一些将它们中的每一个切换为可见的逻辑。由于它不能修改 HTML 按钮的实际启用状态,所有提交处理程序必须询问此类是否“启用”,如果是则取消事件。如果您对此实现感兴趣,我可以与您分享(除非您感兴趣,否则我不想用代码淹没 stackoverflow)。

【讨论】:

  • 您能否提供一个使用 gwt 上传文件的示例工作代码?
  • 我的代码很大,而且它不是(不幸的)开源的,但如果你愿意的话,我可能会分享一些我的聪明的小sn-ps,比如伪按钮类。 -- 但是您的示例代码与我的看起来并没有什么不同。您是否尝试使用“HTML 按钮”技巧?尝试使用 firebug 来查看表单是否实际提交等。
  • GWT 构建起来有点慢,所以我不会费心尝试编译你的代码来测试它...... ;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-25
  • 1970-01-01
  • 1970-01-01
  • 2013-02-21
  • 2013-12-16
  • 2013-04-10
  • 2023-04-09
相关资源
最近更新 更多