如何取消使用BlobService.createBlockBlobFromBrowserFile启动的上传?

时间:2017-12-11 23:15:42

标签: javascript azure-storage azure-node-sdk

我正在使用Microsoft Azure存储客户端库的BlobService.createBlockBlobFromBrowserFile来允许用户将文件上载到Azure存储容器。我想为他们提供取消正在进行的上传的方法,例如:如果它很大并且耗时太长或者他们选择了错误的文件。有什么方法可以做到这一点吗?我在API中看不到任何明显的东西。

我的代码基于these samples,例如

var file = document.getElementById('fileinput').files[0];

var customBlockSize = file.size > 1024 * 1024 * 32 ? 1024 * 1024 * 4 : 1024 * 512;
blobService.singleBlobPutThresholdInBytes = customBlockSize;

var finishedOrError = false;
var speedSummary = blobService.createBlockBlobFromBrowserFile('mycontainer', file.name, file, {blockSize : customBlockSize}, function(error, result, response) {
    finishedOrError = true;
    if (error) {
        // Upload blob failed
    } else {
        // Upload successfully
    }
});
refreshProgress();

SpeedSummary返回的createBlockBlobFromBrowserFile对象是我认为的this one,其中没有类似的内容。

还在MSDN here上询问。

1 个答案:

答案 0 :(得分:2)

MSDN指示我this github issue讨论相同的问题,并使用custom filter作为解决方案。

所以这是我第一次尝试过滤器,可以取消正在进行的上传。这当然不是完美的,但似乎在我的基本测试中起作用。我很乐意从真正熟悉此事的人那里得到反馈。这是取消的正确方法,即只是从回调中返回吗?

我还没有使用任何其他过滤器对其进行测试,例如:重试过滤器。此外,它假设您只有一次上传:它不会重置其状态或预计会有多次上传。

// Filter allowing us to cancel an in-progress upload by setting 'cancel' to true.
// This doesn't cancel file chunks currently being uploaded, so the upload does continue 
// for a while after this is triggered. If the last chunks have already been sent the 
// upload might actually complete (??)
// See http://azure.github.io/azure-storage-node/StorageServiceClient.html#withFilter
var cancelUploadFilter = {
    cancel: false,              // Set to true to cancel an in-progress upload

    loggingOn: true,            // Set to true to see info on console

    onCancelComplete: null,     // Set to a function you want called when a cancelled request is (probably?) complete, 
                                // i.e. when returnCounter==nextCounter. Because when you cancel an upload it can be many 
                                // seconds before the in-progress chunks complete. 

    // Internal from here down

    nextCounter: 0,             // Increments each time next() is called. a.k.a. 'SentChunks' ? 
    returnCounter: 0,           // Increments each time next()'s callback function is called. a.k.a. 'ReceivedChunks'?
    handle: function (requestOptions, next) {
        var self = this;
        if (self.cancel) {
            self.log('cancelling before chunk sent');
            return;
        }
        if (next) {
            self.nextCounter++;
            next(requestOptions, function (returnObject, finalCallback, nextPostCallback) {
                self.returnCounter++;
                if (self.cancel) {
                    self.log('cancelling after chunk received');
                    if (self.nextCounter == self.returnCounter && self.onCancelComplete) {
                        self.onCancelComplete();
                    }

                    // REALLY ??? Is this the right way to stop the upload?
                    return;

                }
                if (nextPostCallback) {
                    nextPostCallback(returnObject);
                } else if (finalCallback) {
                    finalCallback(returnObject);
                }
            });
        }
    }, 
    log: function (msg) {
        if (this.loggingOn) {
            console.log('cancelUploadFilter: ' + msg + ' nc: ' + this.nextCounter + ', rc: ' + this.returnCounter);
        }
    },
};

您可以在创建blob服务时使用它:

var blobService = azure.createBlobService().withFilter(cancelUploadFilter);

然后,如果您正在进行上传,则会将其取消:

cancelUploadFilter.cancel = true;

// optional: if you want to know when all in-progress chunks have stopped: 
cancelUploadFilter.onCancelComplete = function () { console.log('Cancelling complete'); };