Not correct uploading with gwt upload

113 Views Asked by At

I have a problem with gwt-upload: I need onChange Upload to get my object from db; if it exists, the web app can upload file with Servlet, otherwise it will display an alert.

The code with comment runs only in the position with 'It works' comment.

Why doesn't it run in the else block?

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);
1

There are 1 best solutions below

0
pratZ On

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.

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