Interrupt Upload immediately

Hi All,
I am uploading 500mb to 1Gb files in my app, and there is a cancel button to interrupt upload.
Clicking on cancel, it will execute interruptUpload() method but it is not enabling the Upload component immediately.

Thanks in advance

Hi, Tried with ~1GB file and a Button for stopping the upload. Upload was interrupted and upload button enabled quite quickly after clicking the button (from 100ms-1.5s). Do you have more information of the issue? How slow it is in your case?

Button stopButton = new Button("stop");
        stopButton.setImmediate(true);
        stopButton.addClickListener(new ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                System.out.println("stop clicker");
                upload.interruptUpload();
            }
        });

Hi there! Also enable push to make sure the browser will be updated as soon as possible. Without push, it may take more then few seconds while the user have no clue why the upload don’t cancel.

To enable the push, add the @Push annotation on the UI class.

This sample should perform well:

@Push
public class UploadFile extends UI {

    @WebServlet(value = "/*", asyncSupported = true)
    @VaadinServletConfiguration(productionMode = false, ui = UploadFile.class)
    public static class Servlet extends VaadinServlet {
    }

    private Upload upload;

    @Override
    protected void init(VaadinRequest request) {
        upload = new Upload("Upload a file", new FileReceiver());

        VerticalLayout layout = new VerticalLayout();

        layout.addComponent(upload);

        Button stopButton = new Button("Stop");
        stopButton.setImmediate(true);
        stopButton.addClickListener(new ClickHandler());
        layout.addComponent(stopButton);

        setContent(layout);
    }

    private class ClickHandler implements ClickListener {

        @Override
        public void buttonClick(ClickEvent event) {
            upload.interruptUpload();
        }
    }

    private class FileReceiver implements Receiver {

        @Override
        public OutputStream receiveUpload(String filename, String mimeType) {

            try {
                File file = new File(filename);
                if (file.exists() || file.createNewFile()) {
                    return new FileOutputStream(filename);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }
    }

}

Also you can try other Upload components from the addons directory. Just search for “Upload” and you’ll find some more:
https://vaadin.com/directory#browse:vaadin,search=Upload