如何取消以BlobService.createBlockBlobFromBrowserFile开始的上传? [英] How to cancel an upload started with BlobService.createBlockBlobFromBrowserFile?

查看:54
本文介绍了如何取消以BlobService.createBlockBlobFromBrowserFile开始的上传?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Microsoft Azure存储客户端库的 BlobService.createBlockBlobFromBrowserFile 允许用户将文件上传到Azure存储容器.我想为他们提供一种取消正在进行的上传的方式,例如万一它太大而用的时间太长,或者他们选择了错误的文件.我有什么办法吗?我在API中看不到任何明显的东西.

I'm using Microsoft Azure Storage Client Library's BlobService.createBlockBlobFromBrowserFile to allow users to upload files to an Azure Storage container. I'd like to provide a way for them to cancel an in-progress upload, e.g. in case it's big and taking too long or they chose the wrong file. Is there any way I can do this though? I can't see anything obvious in the API.

我的代码基于这些示例,例如

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();

我认为从 createBlockBlobFromBrowserFile 返回的 SpeedSummary 对象是

The SpeedSummary object returned from createBlockBlobFromBrowserFile is I think this one, which doesn't have anything like that available.

也在推荐答案

MSDN将我定向到

MSDN directed me to this github issue discussing the same question, and using a custom filter as a solution.

因此,这是我首次尝试使用过滤器,该过滤器可让您取消正在进行的上传.这当然不是完美的,但似乎在我的基本测试中起作用.我很想从实际熟悉此事的人那里获得反馈.这是取消的正确方法,即仅从回调内返回吗?

So here's my first attempt at a filter that lets you cancel an in-progress upload. This is certainly not perfect, but appears to work in my rudimentary testing. I'd love to get feedback from people actually familiar with this. Is this the right way to cancel, i.e. just return from within the callback?

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

I haven't tested it with any other filters applied, e.g. a retry filter. Also it assumes you only ever have one upload: it doesn't reset its state or expect multiple uploads.

// 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服务时使用它,如下所示:

You would use it when creating the blob service like this:

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

然后,如果您正在进行上传,则可以将其取消,就像这样:

Then if you have an upload in progress you cancel it like so:

cancelUploadFilter.cancel = true;

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

这篇关于如何取消以BlobService.createBlockBlobFromBrowserFile开始的上传?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆