Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    FormSubmitFailedHandler is not called when file upload with UploadItem and submitForm() fails

    Hi,
    for a simple file upload i'm using an UploadItem with a "native submit".

    The file upload is working in general, but in case of a failure the handler added with addFormSubmitFailedHandler() is not invoked.
    Complete example code:

    Code:
    public class TestEntryPoint implements EntryPoint {
        private static final Logger logger = Logger.getLogger(TestEntryPoint.class.getName());
    
        @Override
        public void onModuleLoad() {
            DynamicForm dynamicForm = new DynamicForm();
            dynamicForm.setAutoFocus(true);
    
            final DynamicForm uploadForm = new DynamicForm();
            uploadForm.setEncoding(Encoding.MULTIPART);
            uploadForm.setMethod(FormMethod.POST);
            // set hidden upload frame as target. Prevents reloading page.
            uploadForm.setTarget("upload_frame");
            uploadForm.setCanSubmit(true);
    
            // event never fired
            uploadForm.addFormSubmitFailedHandler(event -> logger.warning("Upload failed"));
    
            UploadItem fileItem = new UploadItem();
            fileItem.setShowTitle(false);
            fileItem.setMultiple(false);
    
            IButton uploadButton = new IButton("Upload!");
            uploadButton.addClickHandler(e -> startFileUpload(uploadForm));
            uploadForm.setItems(fileItem);
    
            Layout layout = new VLayout();
            layout.setAutoHeight();
            layout.setMembers(uploadForm, uploadButton);
    
            CanvasItem canvasItem = new CanvasItem();
            canvasItem.setCanvas(layout);
    
            dynamicForm.setItems(canvasItem);
            dynamicForm.draw();
        }
    
        private void startFileUpload(DynamicForm uploadForm) {
            logger.info("Start file upload");
            uploadForm.setAction("http://localhost:8080/upload_endpoint");
            uploadForm.submitForm();
        }
    }
    Tested:
    * the file is removed after choosing it in upload item but before hitting the upload button
    * the submit URL is wrong / server is not reachable
    * server responds with HTTP error code

    In all cases the FormSubmitFailedHandler is not called.

    The result frame set here with setTarget() is a hidden frame in the index.html. ( iframe height="0" name="upload_frame"></iframe> )
    If it is not set, the page is reloading and it is unclear if the handler was invoked.
    I'm not sure if the FormSubmitFailedHandler can even be used here in this case (with or without target frame). The documentation is unclear about this.


    v12.0p_2018-07-12/LGPL Development Only (built 2018-07-12)
    Chrome 70
    Last edited by abika; 13 Nov 2018, 06:09.

    #2
    If you are not using our server framework, this handler can't be used. As we cover in the docs, the way you're going to get your code invoked when the server responds is to have the server write out JavaScript in the response that invokes your code. As far as detecting that the server was unreachable or the app is not deployed correctly, the best way is via timeout.

    Comment


      #3
      Thanks for you answer. Then I'll try to find another solution instead of the FormSubmitFailedHandler.

      The Javadoc for com.smartgwt.client.widgets.form.DynamicForm#addFormSubmitFailedHandler explicitly stats the handler is invoked for a native submit via submitForm(). This is wrong then. The server side is not mentioned here or for submitForm().

      Comment

      Working...
      X