ajax - How to cancel a file upload using dojo.io.iframe.send? - Stack Overflow

admin2022-06-12  127

I am using the dojo.io.iframe.send method to send a file to my server. I want to provide a way that the user can cancel the send once it is in progress, in case it is taking too long or the user realizes she sent the wrong file.

I can't figure a way to do this. I could use a timeout to terminate the send if it is taking a long time (that is, the server does not respond quickly), but that is not what I want. I want to terminate any time the user makes a gesture (such as clicking a "Cancel" button.

Thanks!

Answers:

I am using the dojo.io.iframe.send method to send a file to my server. I want to provide a way that the user can cancel the send once it is in progress, in case it is taking too long or the user realizes she sent the wrong file.

I can't figure a way to do this. I could use a timeout to terminate the send if it is taking a long time (that is, the server does not respond quickly), but that is not what I want. I want to terminate any time the user makes a gesture (such as clicking a "Cancel" button.

Thanks!

Answers:

For security reasons JavaScript cannot even access the file you are sending up. All file uploads are done using a form and handled by the browser directly. To my best knowledge there is no direct interface to monitor the progress of upload, nor the way to affect it (e.g., cancel it).

Navigating away may help, but it is still up to the browser to interrupt the transfer or not — there is no specification for that.

To sum it up: not possible to do it reliable in JavaScript in the portable fashion. That means Dojo does not provide this functionality.

Answers:

Hi i realize this is a rather late reply so i hope you or someone who stumbled here through google will have some use for this.

It is possible to cancel the file upload in the iFrame, in Firefox you can call the method window.stop() on the iFrame. IE however doesn't have this method but comes with the execCommand("Stop") however since manually pressing stop doesn't even work the scripted stop wont either.

My first attempts involved setting the location of the iFrame to about:blank to no avail sadly. However browsing to a source that has a body will cancel the iFrame POST!

Answers:

So I realize this is a really old post (4 years!) that I'm answering. But, I found it when googling for an answer that should have, in retrospect, been obvious, but isn't answered here (nor anywhere else I looked). So, for the sake of the next poor schlub:

If (like me), you don't care about actually stopping the file transfer, but just want to allow the dojo.io.iframe to MOVE ON with life (effectively cancelling from the user's perspective), you can simply ignore the result of the deferred callbacks. However, if you also want to then upload additional things via dojo.io.iframe, you must also make this dojo.io.iframe also ignore the result. To do this, it's annoying simple...the dojo.io.iframe.send() call returns a deferred. If you want to cancel the upload such that the dojo.io.iframe will perform subsequent queued send calls (since it can only do one at a time), you just need to call a .cancel() on the deferred that came back from the send that is currently in progress.

As in:

    var dfd = dojo.io.iframe.send({...});
    var dfd2 = dojo.io.iframe.send({...}); // this send will not start until the first one completes. it is blocked internally by dojo.io.iframe.

...

Now, the user clicks a cancel button somewhere because the upload is taking too long or they realized they didn't mean to upload the chosen file. We don't want to wait until the first file transmission is done, because we don't care about that transmission result anymore.

onclick: function() {
    dfd.cancel();
}

Calling the cancel on the dfd will allow the second send() call to execute immediately. if you watch in fiddler, you will see that the second upload now begins immediately, rather than waiting for the first (cancelled) one to complete, as it would have done without calling the .cancel() on the deferred.

Without cancelling the first deferred, the dojo.io.iframe will continue to dutifully wait for the result of the first upload, thereby blocking any subsequent queued up send() calls waiting to be processed through the dojo.io.iframe.

Answers:

What a friend did was simply reloading the webpage when the cancel button was preseed (I think by calling document.location.refresh function). There IS also a way of monitoring the progress o an upload (having a progress bar), but it's server dependent (in the case I saw, it was an apache module, then you would poll for the progress on a specified link and the server would return the progress).

转载请注明原文地址:https://www.u19.cn/read-143541.html

New Post(0)