显示 Azure BLOB 的上传进度

Ill*_*lep 2 c# azure azure-blob-storage

以下代码演示了将文件上传到 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);
Run Code Online (Sandbox Code Playgroud)

Gau*_*tri 7

请尝试如下操作:

//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()
};
Run Code Online (Sandbox Code Playgroud)

  • 我不知道,抱歉。但是,如果我进行此上传,我会选择使用共享访问签名从浏览器直接上传到 Azure 存储,而不是从此 Web 层进行上传。 (2认同)