如何中止上传到Google云端硬盘?

时间:2016-03-06 19:34:52

标签: javascript google-drive-api

我使用以下示例将文件上传到Google云端硬盘。如何中止上传?在构造函数中将xhr.abort()变量设置为xhr之后,我尝试设置null

var MediaUploader = function (options) {
    var noop = function () { };
    this.file = options.file;
    this.contentType = options.contentType || this.file.type || 'application/octet-stream';
    this.metadata = options.metadata || {
        'title': this.file.name,
        'mimeType': this.contentType
    };
    this.token = options.token;
    this.onComplete = options.onComplete || noop;
    this.onProgress = options.onProgress || noop;
    this.onError = options.onError || noop;
    this.offset = options.offset || 0;
    this.chunkSize = options.chunkSize || 0;
    this.retryHandler = new RetryHandler();
    this.xhr = null;
    this.url = options.url;
    if (!this.url) {
        var params = options.params || {};
        params.uploadType = 'resumable';
        this.url = this.buildUrl_(options.fileId, params, options.baseUrl);
    }
    this.httpMethod = options.fileId ? 'PUT' : 'POST';
};
MediaUploader.prototype.Cancel= function()
{
    this.xhr.abort();
}

https://github.com/googledrive/cors-upload-sample

实际上,如果您看到代码here

xhr不是类变量;每次调用不同的函数时,总是使用另一个实例创建它,例如,如果您看到:

MediaUploader.prototype.upload = 
    function() { 
        var self = this; 
        var xhr = new XMLHttpRequest();
    };

所以我所做的就是将xhr移动到一个类变量,这样在我调用uploader.Cancel时它就不会为空。在这些更改后,代码变为如下:

MediaUploader.prototype.upload = 
    function() { 
        var self = this; 
        this.xhr = new XMLHttpRequest();
    };

我更改了所有功能的代码,并使用this.xhr= new XMLHttpRequest();创建var xhr = new XMLHttpRequest();,但这不起作用。我仍然无法取消上传..

0 个答案:

没有答案