2

这是迄今为止生成的 html(GWT 用作前端),到目前为止,我所拥有的复制了 GWT FileUpload类。

<input type="file" id="input" onchange="handleFiles(this.file)">

在 的帮助下可以正常工作HandleFileUploadServet.java,因为 java 作为后端。

使用 addSubmitCompleteHandler

form.addSubmitCompleteHandler(new SubmitCompleteHandler() {..

这相当于

 .submit(function(){
   //handle file response
})

工作正常。

这是问题,上传文件时如果互联网断开,浏览器没有 error/exception/response抛出。

我想通知用户,存在网络问题。

但是浏览器继续提交表单并且没有从该状态返回。

有什么提示吗?

谢谢你的时间。

4

1 回答 1

0

如果事件为空,请检查您的处理程序吗?

form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
        public void onSubmitComplete(SubmitCompleteEvent event) {
            if(event != null){
                   Window.alert("Upload OK!");
            }else
                   Window.alert("Upload fail");
    });

但我认为,如果您遇到网络问题,则永远不会触发 SubmitCompleteEvent。

一个解决方案可以是在您提交文件时制作一个计时器:

public class ViewWidget {

Form form;
Timer timer = new Timer() {
     @Override
     public void run() {
         Window.alert("Troubles with upload! Try again!");
     }
 };

 public ViewWidget(){
     form.addSubmitHandler(new SubmitHandler() {

        @Override
        public void onSubmit(SubmitEvent event) {
            timer.schedule(10000);
        }
    });

     form.addSubmitCompleteHandler(new SubmitCompleteHandler() {

        @Override
        public void onSubmitComplete(SubmitCompleteEvent event) {
            //Cancel the timer
            timer.cancel();

            if(event != null){
                //Do your Stuff
                Window.alert("Upload Ok !");
            }else
                Window.alert("Upload Fails");
        }
    });
 }

我不尝试代码,但它应该可以工作。

希望能帮助到你。

于 2014-03-14T20:26:28.110 回答