2013-09-16 26 views
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

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

但浏览器不断提交表单而不是从该状态返回。

任何提示?

谢谢你的时间。

回答

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"); 
     } 
    }); 
} 

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

希望它有帮助。