简体   繁体   English

用gwt上传不正确的上传

[英]Not correct uploading with gwt upload

I have a problem with gwt-upload: I need onChange Upload to get my object from db; 我的gwt-upload有问题:我需要onChange Upload才能从db获取对象; if it exists, the web app can upload file with Servlet, otherwise it will display an alert. 如果存在,则Web应用程序可以使用Servlet上传文件,否则它将显示警报。

The code with comment runs only in the position with 'It works' comment. 带注释的代码仅在带“ It works”注释的位置运行。

Why doesn't it run in the else block? 为什么它不能在else块中运行?

final MultiUploader upload = new MultiUploader(FileInputType.BUTTON);
upload.addOnChangeUploadHandler(new OnChangeUploaderHandler() {

    @Override
    public void onChange(IUploader uploader) {
        myService.getMyObject(name, new AsyncCallback<List<Object>>() {

            @Override
            public void onFailure(Throwable caught) {
                Window.alert("Something");
            }

            @Override
            public void onSuccess(List<Object> listMyObject) {
                if(listMyObject.size() == 0) {
                    Window.alert("Error.");
                } else {
                    //It doesn't works.
                    String url = GWT.getModuleBaseURL() + "upload?nameObject=" + name;
                    upload.setServletPath(url);
                }
            }
        });
    }
});
//It works.
String url = GWT.getModuleBaseURL() + "upload?nameObject=" + name;
upload.setServletPath(url);

You can solve this by adding an upload button and submitting the uploader form on success of the server call. 您可以通过添加上载按钮并在服务器调用成功后提交上载器表单来解决此问题。 Here's an outline on how to do it. 以下是有关操作方法的概述。 Read gwtupload documentation for adding servlet mapping in web.xml. 阅读gwtupload文档,以在web.xml中添加servlet映射。

Uploader uploader = new Uploader(FileInputType.BROWSER_INPUT);
uploader.setServletPath("myservletpath");
uploader.setAutoSubmit(false);
uploader.setEnabled(true);

Button uploadButton = new Button("Upload");
uploadButton.addClickHandler(new ClickHandler()){
    myService.getMyObject(name, new AsyncCallback<List<Object>>() {

            @Override
            public void onFailure(Throwable caught) {
                //do something
            }

            @Override
            public void onSuccess(List<Object> listMyObject) {
                //do something if condition satisfies then submit
                if(conditionSatifies){
                    uploader.submit();
                }else{
                    //do something, prompt a message
                }
            }
     });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM