Upload: how would you ask confirmation to override file?

Hi guys,

I’m using the Upload component which works very well.

I’d like to show a warning to the use if the file he uploads already exists on the server. Something like
“This file already exists, are you sure to overwrite it?”
.

The easiest for me would be to have

  • an upload component sending an click event to the server telling the user would like to upload,
  • then the server checks that the file already exists, and sends a modal confirmation window to the client,
  • then the client to press the “Yes” button of the window,
  • then the server ClickListener would tell the browser, ok start upload.
    But the Upload component is not designed that way I think.

So, I don’t see how to do that.
If I wait for the upload to start, it’s too late to open a new modal Window. In Swing, I could transfer the upload in a suspended other thread and use the UI thread to show a confirmation modal window in another thread. But here, with the request/response web model, I’m not sure it would work.

Has anybody a simple/brilliant idea for me?

Actually, I thought that the Upload component uses a separate thread for uploading data. Matti can probably confirm this?

So, it should be possible to listen Upload.StartedEvent and display a confirmation dialog. It probably takes some time before user actually cancels the upload (clicks the button in dialog) but the event should be parallel to ongoing upload. Button click listener (or similar) could then cancel the upload by calling the Upload.interruptUpload(). (I think this function first came in the latest 6.1.0)

Something like this:


        Upload.Receiver receiver = new Upload.Receiver() {

            public OutputStream receiveUpload(String filename, String MIMEType) {
                // TODO: Return your upload stream here
                return null;
            }
        };
        final Upload upload = new Upload("Upload", receiver);
        
        // Listener for upload start
        upload.addListener(new Upload.StartedListener() {

            public void uploadStarted(StartedEvent event) {
                
                // Just some prototype code assuming interface like ConfirmCallback exists.
                confirmFileOverwrite(event.getFilename(), new ConfirmCallback() {

                    public void cancelled() {
                        // Cancel the ongoing upload
                        upload.interruptUpload();
                    }
                    
                    public void confirmed() {
                        // Nothing here. Just continue uploading. Rest of the handling in Upload.FinishedListener.
                    }

                });
            }
        });
        addComponent(upload);

My guess is that there might be a problem here with the Upload.StartedEvent being sent from the “upload thread” rather than the “ui thread” and the UI updates you make there do not show.

Note to myself: Test this. :slight_smile:

Thank you Sami.

Your answer makes me think that my problem is probably simpler than I thought.
To me it’s no trouble to ask for the confirmation after the file transfer, I just have to target the transfer to a temporary location and move it at the end, after a confirmation, to override the real resource.

John.

Yes, if you really can wait until the end of the upload process there shouldn’t be any problems. I just noticed the interruptUpload function the first time now and thought that this might be the “right” way to do this.

I haven’t yet tried the interruptUpload-function myself, but if it does not work the way speculated above I’m ready to even submit some tickets :slight_smile:

Have you implemented a Receiver? You can do your check in the receiveUpload - method if filename is enough for you. If you don’t have a file by that name, then return the outpuStream you want your upload to use.

Yes, it was my first reflex. But what to do if that file already exists and I need a confirmation from the user. Such a confirmation would require another request/response lifecycle and being in the Receiver would probably be too

I need to show the popup for renaming the file name after upload button click. This popup will show a initial file name. if user change the file name, image should be saved with new file name otherwise same file name that is initially present on the popup.
How to stop the default upload before popup is closed ?

rename file?? in recive??

Did anybody found the answer for Rahul’s question above ? Even my requirement is to ask user if he wants to rename the file before uploading. It not… continue with the same name.