Cancel, Abort and Retry individual file upload with ResumableJS

5.2k views Asked by At

I've successfully managed to upload multiple files in chunks to a server using ResumableJS. During the upload process the user is able to see the overall upload progress and the individual file upload percentage. It's also possible to pause/resume the overall upload.

What i would like to now is allow the user to cancel/abort an individual file upload without interrupting the other file uploads.
In ResumableJS website there are some methods that allow to do what i want, but no examples on how to accomplish this.
I have tried the following:

onclick="ResumableFile.abort(); return(false);"
onclick="file.abort(); return(false);"
onclick="this.abort(); return(false);"

How may i abort a specific file upload without interrupting the overall file upload?

UPDATE: Here is my JS code:

var r = new Resumable({
    target: 'FileHandler.ashx'
});

// Resumable.js isn't supported, fall back on a different method
if (!r.support)
{}
else
{
    // Show a place for dropping/selecting files
    $('.resumable-drop').show();
    r.assignDrop($('.resumable-drop')[0]);
    r.assignBrowse($('.resumable-browse')[0]);

    // Handle file add event
    r.on('fileAdded', function (file)
    {
        //// Add the file to the list
        $('.resumable-list').append('<li class="resumable-file-' + file.uniqueIdentifier + '">Uploading <span class="resumable-file-name"></span> <span class="resumable-file-progress"></span> <button type="button" id="removeButton" onclick="abortFile();">Remove</button>');
        $('.resumable-file-' + file.uniqueIdentifier + ' .resumable-file-name').html(file.fileName);

        // Actually start the upload
        r.upload();
    });

    //var file = new ResumableFile();

    //$("#removeButton").on("click", function ()
    //{
    //    console.log("abort!");
    //    file.abort();
    //});

    function abortFile()
    {
        console.log("abort!");
        r.abort();
    }

    r.on('pause', function ()
    {
        // Show resume, hide pause main progress bar
    });

    r.on('complete', function ()
    {
        // Hide pause/resume when the upload has completed
    });

    r.on('fileSuccess', function (file, message)
    {
        // Reflect that the file upload has completed
    });

    r.on('fileError', function (file, message)
    {
        // Reflect that the file upload has resulted in error
    });

    r.on('fileProgress', function (file)
    {
        // Handle progress for both the file and the overall upload
    });

}


With Ruben Rutten's help here is how i solved my issue:

// Handle file add event
r.on('fileAdded', function (file)
{
    // Show progress bar

    // Show pause, hide resume

    //// Add the file to the list
    $('.resumable-list').append('<li class="resumable-file-' + file.uniqueIdentifier + '">Uploading <span class="resumable-file-name"></span> <span class="resumable-file-progress"></span> <button type="button" class="removeButton" id="' + file.uniqueIdentifier + '">Remove</button>');
    $('.resumable-file-' + file.uniqueIdentifier + ' .resumable-file-name').html(file.fileName);


    ///event to remove file from upload list
    $(".removeButton").on("click", function ()
    {
        for (var i = 0; i < r.files.length; i++)
        {
            var identifier = $(this).attr("id");

            if (r.files[i].uniqueIdentifier == identifier)
            {
                r.files[i].cancel();
                $('.resumable-file-' + identifier).remove();
            }
        }
    });

    r.upload();

});
2

There are 2 answers

5
Ruben On BEST ANSWER

If you have your JavaScript file / part, you create a var r = new Resumable(). You should create a function that aborts it, such as

function abortFile() {
    r.abort();
}

Then on your button, use onclick="abortFile(); return false;"

I haven't tested it, but it should work.

In case you use jQuery, you could do the following:

<button id="cancelButton">Cancel</button>

<script type="text/javascript">
    // Not sure if this works, just to test
    var file = new ResumableFile();

    $("#cancelButton").on("click", function() {
        file.abort();
    });
</script>
0
Matto On

I know this is an old question, but this might help someone:

Looking at your solution, there is a more efficient way to obtain the resumable instance than iterating all of the files:

// event to remove file from upload list
$(".removeButton").on("click", function ()
{
    var identifier = $(this).attr("id");
    var file = resumable.getFromUniqueIdentifier(identifier);
    file.cancel();
    $('.resumable-file-' + identifier).remove();
});