cilphex
cilphex

Reputation: 6096

What's the best way to cancel an upload in progress using filepicker.io?

I'm using filepicker.makeDropPane to play around with some simple uploads. One thing that I cannot find in the docs at filepicker.io's web documentation is a method for canceling an upload that is in progress.

Intuitively I feel that the onStart function should be passed an additional parameter. An object that represents the upload that has a cancel() function which when called would immediately cancel the file uploads. Something like this does not seem to be available.

Upvotes: 3

Views: 315

Answers (2)

bearfriend
bearfriend

Reputation: 10421

Here's an ugly hack I figured out to protect against unwanted returned files as well some other fancy stuff around progress and multiple files etc. You definitely want to use a static js file include so it doesn't get changed on you and break this.

Use this in your progress handler:

var event = event || window.event;

// For firefox, go and grab the XHR progress event object.                                                                                                                                                                                
if (typeof event == 'undefined' || event == null) {
    if (typeof arguments.callee.caller.arguments[0] == 'object') {
        event = arguments.callee.caller.arguments[0];
    }
    else {
        event = arguments.callee.caller.arguments.callee.caller.arguments.callee.caller.arguments[0];
    }
}

var sUploadId;

     if (event.type.toLowerCase() == 'progress') {
        // if we haven't already tagged this XHR object (one per file), tag it now                                                                                                                                                 
        // if we receive further progress from this file, it will already have been tagged                                                                                                                                                

        if (!('id' in event.currentTarget)) {
            sUploadId = goUploadProgress.files.length;
            event.currentTarget.id = sUploadId;
            goUploadProgress.files.push({id: sUploadId, total:event.total});
        }
        else {
            sUploadId = event.currentTarget.id;
        }

        // get the loaded bytes for this file                                                                                                                                                                                             
        goUploadProgress.files[sUploadId].loaded = event.loaded;
    }
    else {
        // ignore readystatechange events                                                                                                                                                                                                  
        return;
    }

I modified this to make more sense for this context, so I may have made a logical mistake, but this is the gist. You can get the event in Firefox by looking up the call stack, and you can append an id to each file's XHR upload object, which will exist until it finishes.

I repeat, this is a hack! Don't trust that it won't break.

Upvotes: 0

brettcvz
brettcvz

Reputation: 2381

we don't actually have this functionality yet, but it makes sense. I'll take a look at adding something along these lines and let you know

Upvotes: 2

Related Questions