上传文件时处理网络问题

Handling network issues while uploading files

本文关键字:网络 问题 处理 文件      更新时间:2023-09-26

这是到目前为止生成的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

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

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

有什么提示吗?

谢谢你的时间。

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

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

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

希望对您有所帮助。