Glenn Stowe
Glenn Stowe

Reputation: 33

Is there any way to cancel an in-progress upload in dojo?

I have a file uploader that handles very large files. Lots of times the users will cancel an upload. There does not appear to be any way to make the browser cancel the XHR POST that is handling it. Consequently, progress and complete events fire much later and the upload actually completes. Presumably there must be an XHR object embedded in the uploader somewhere that I could call abort on, but I see nothing in the API docs, or in a console dump of the uploader object.

Upvotes: 2

Views: 227

Answers (1)

ben
ben

Reputation: 3568

If your goal is to support only the HTML5 uploader (and not the Iframe or the Flash version), then you can do something like that:

  • Create a new uploader widget with the following (code not tested, might need some adjustment):

define(['dojo/_base/declare', 'dojox/form/Uploader'], function(declare, Uploader) {
  return declare([Uploader], {
    xhrRequest: null,
    createXhr: function() {
      this.xhrRequest = this.inherited(arguments);
      return this.xhrRequest;
    },
    cancel: function() {
      if (this.xhrRequest && this.xhrRequest.abort) {
        this.xhrRequest.abort();
      }
    }
  });
});

  • Use your new widget instead of dojox.form.Uploader
  • Call the cancel method when you want to cancel the upload.

But remember: this will work only with the HTML5 uploader.

Upvotes: 1

Related Questions