微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

显示上传到 Azure BLOB 的进度

如何解决显示上传到 Azure BLOB 的进度

以下代码演示了将文件上传到 Azure BLOB。因此,我将上传较大的文件,因此可能会因为带宽问题而导致上传失败(在 SO 中讨论)。

以下代码工作正常,但我需要一种方法以百分比的形式向最终用户显示上传进度。如何修改我的代码以使其成为可能?

BlobUploadOptions op = new BlobUploadOptions
{
    TransferOptions = new StorageTransferOptions
    {
        MaximumTransferSize = 4 * 1024 * 1024,InitialTransferSize = 4 * 1024 * 1024,MaximumConcurrency= 5
     },HttpHeaders = new BlobHttpHeaders
     {
        ContentType = request.Request.ContentType
     }
};

Azure.Response<BlobContentInfo> ci = await 
   blobClient.UploadAsync(fileStream,op);

解决方法

请尝试以下操作:

//Define a progress handler.
class MyProgressHandler : IProgress<long>
{
    public void Report(long value)
    {
        //Do something to report the progress. I am simply printing the bytes uploaded.
        Console.WriteLine($"Bytes uploaded: {value}");
    }
}

//Use the progress handler in your upload options.
BlobUploadOptions op = new BlobUploadOptions
{
    TransferOptions = new StorageTransferOptions
    {
        MaximumTransferSize = 4 * 1024 * 1024,InitialTransferSize = 4 * 1024 * 1024,MaximumConcurrency= 5
     },HttpHeaders = new BlobHttpHeaders
     {
        ContentType = request.Request.ContentType
     },ProgressHandler = new MyProgressHandler()
};

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。