2011-06-20 109 views
2

我想在我的应用程序中上传文件,并且想要设置文件在上传到本地系统后应该保存的路径。我使用下面的代码,但在提交按钮时没有得到响应,而单击。请告诉我,在gwt文件上传工作正常的代码。 [代码]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); 
    } 
} 

感谢

爱玛

+0

文件是否上传?我自己使用GWT时遇到了一些使用GWT的fileupload问题,并且上传工作正常,但回调没有被触发。我会尽力找到我所做的修复它。 (我记得最后我写了自己的服务处理它在后端,虽然) –

+0

没有文件不上传我不调用服务器端的代码它是客户端Java程序,所以没有问题调用代码。我不知道如何设置上传文件的位置。 –

+0

@stein我想回调没有触发的原因是:你可能没有设置'formPanel.errorReader'。如果你的代码和amandeep的代码一样。 – RAS

回答

1

我一直在使用这个Link

这真的是我一直在寻找最终解决了这个问题。

0

现在,我还记得。有在这导致form.submit()没有上下班FormPanel中代码中的错误,当窗体的类型从改变默认(不知道它是否已经在任何GWT版本中修复)。如果您创建如下的“原生”提交按钮:

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

它将提交表单。

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

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

+0

你能否提供一个使用gwt上传文件的示例工作代码? –

+0

我的代码非常大,并且不是(不幸的是)开源,但是如果你喜欢伪按钮类,我可能会共享我的一些小巧的片段。 - 但你的示例代码与我的看法不一样。您是否尝试使用“HTML按钮”技巧?尝试使用萤火虫,看看表单是否实际提交等。 –

+0

GWT是有点慢,所以我不打扰试图编译你的代码来测试它;;) –

0

您可能会感兴趣的link。虽然它使用图片进行上传,但您可以将其用于任何类型的文件上传并进行一些小的更改。