Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

catching a completed abort of file upload #165

Closed
project707 opened this issue Mar 19, 2014 · 5 comments
Closed

catching a completed abort of file upload #165

project707 opened this issue Mar 19, 2014 · 5 comments

Comments

@project707
Copy link

Is there an easy way to fire some custom actions once the cancelled upload has actually stopped?

If I wrap the call to $upload.abort in my own function it doesn't work as the transfer is often still in progress for a moment after I call it and I don't want to resort to setting timeouts.

@danialfarid
Copy link
Owner

Good suggestion, I will implement it for the next version.
For now you can do this hack:

var config = {url:'blah', file:myfile, ...}
$upload.upload(config);

config.__XHR.addEventListener("abort", function() {
    console.log('abort is done');
}, false);
$upload.abort();

It would only work for HTML5 browsers only.

@project707
Copy link
Author

Thanks for the great uploader code and the quick response on this request.

I tried that hack and it needed to be yet a little more hacky, because when this is first executed __XHR does not always exist. Anyhow (for anyone trying to implement this for the time being) the following works for me:

var config = {url:'blah', file:myfile, ...}
var xhrListenerSet = false;

$upload.upload(config)
    .progress(function(evt) {

        if (!xhrListenerSet) {
            config.__XHR.addEventListener("abort", function() {
                console.log('abort is done');
            },false);

            xhrListenerSet = true;
        }
    });
$upload.abort();

@project707
Copy link
Author

So it turns out this is not as simple as I thought. Now that I have been able to test this it seems progress will still fire after the "abort" event listener has fired.

I think this feature may still be useful for many cases, but for my particular case where I have checks in .progress to set states and then want to reset those states once abort has fired it doesn't work so well. I suppose setting things every time .progress fires is a bit of a hack in itself, but it doesn't seem that there is an upload.start method to allow for doing this just once.

@danialfarid
Copy link
Owner

try onloadstart and onloadend instead of abort.

@danialfarid
Copy link
Owner

The new version 1.2.10 has this function to enable attach arbitrary listeners to XHR.

$upload.upload(config).then(...).xhr(function(xhr) {
     xhr.upload.addEventListener('abort', function(){console.log('aborted complete')}, false);
})

The Demo page already include this function.

danialfarid added a commit that referenced this issue Mar 24, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants