Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .NET Core 2.0 MVC文件上载进度_C#_Asp.net Mvc_File Upload_Asp.net Core Mvc_Azure Storage Blobs - Fatal编程技术网

C# .NET Core 2.0 MVC文件上载进度

C# .NET Core 2.0 MVC文件上载进度,c#,asp.net-mvc,file-upload,asp.net-core-mvc,azure-storage-blobs,C#,Asp.net Mvc,File Upload,Asp.net Core Mvc,Azure Storage Blobs,因此,我花了很长时间试图为我的.Net核心MVC应用程序显示进度条,而官方文档并没有太大帮助 此处的文档: 我还想在文件到达我的控制器时将其上载到Azure blob存储。 用户可以上传任意数量的文件 这是我上传的代码: for (int i = 0; i < videoFile.Count; i++) { long totalBytes = videoFile[i].Length; byte[]

因此,我花了很长时间试图为我的.Net核心MVC应用程序显示进度条,而官方文档并没有太大帮助

此处的文档:

我还想在文件到达我的控制器时将其上载到Azure blob存储。 用户可以上传任意数量的文件

这是我上传的代码:

for (int i = 0; i < videoFile.Count; i++)
            {

                long totalBytes = videoFile[i].Length;

                byte[] buffer = new byte[16 * 1024];
                using (Stream input = videoFile[i].OpenReadStream())
                {
                    long totalReadBytes = 0;
                    int readBytes;

                    while ((readBytes = input.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        totalReadBytes += readBytes;
                        var progress = (int)((float)totalReadBytes / (float)totalBytes * 100.0);
                    }
                }

                String videoPath = videoFile[i].FileName;

                await sc.UploadBlobAsync(groupContainer, videoPath, videoFile[i]);
            }
for(int i=0;i0)
{
totalReadBytes+=readBytes;
变量进度=(int)((浮点)totalReadBytes/(浮点)totalBytes*100.0);
}
}
字符串videoPath=videoFile[i]。文件名;
等待sc.UploadBlobAsync(groupContainer、videoPath、videoFile[i]);
}
下面是我的UploadBlobAsync方法:

public async Task<bool> UploadBlobAsync(string blobContainer, string blobName, IFormFile file) {
        CloudBlobContainer container = await GetContainerAsync(blobContainer);
        CloudBlockBlob blob = container.GetBlockBlobReference(blobName);

        CancellationToken cancellationToken = new CancellationToken();
        IProgress<StorageProgress> progressHandler = new Progress<StorageProgress>(
            progress => Console.WriteLine("Progress: {0} bytes transferred", progress.BytesTransferred)
        );

        using (var filestream = file.OpenReadStream()) {
            await blob.UploadFromStreamAsync(filestream,
                default(AccessCondition),
                default(BlobRequestOptions),
                default(OperationContext),
                progressHandler,
                cancellationToken);
        }
        return true;
    }
public异步任务上传BlobAsync(字符串blobContainer、字符串blobName、IFormFile){
CloudBlobContainer容器=等待GetContainerSync(blobContainer);
CloudBlockBlob blob=container.GetBlockBlobReference(blobName);
CancellationToken CancellationToken=新的CancellationToken();
IProgress progressHandler=新进度(
progress=>Console.WriteLine(“progress:{0}字节已传输”,progress.ByTestTransfered)
);
使用(var filestream=file.OpenReadStream()){
等待blob.UploadFromStreamAsync(文件流,
默认(访问条件),
默认值(BlobRequestOptions),
默认值(OperationContext),
progressHandler,
取消令牌);
}
返回true;
}
我想知道的是:

  • 据我所知,我需要做两个进度条,一个是从客户端到服务器,另一个是从服务器到azure。这是正确的吗

  • 如何在前端显示每个文件的进度?我可以想象这是一个ajax请求,请求我在控制器中设置的列表

  • 当文件已经缓冲时,我是否正在读取while循环中的字节?如果我可以访问文件流,那么文件不是已经在服务器上缓冲了吗

  • 当Azure的IProgress实现发生更改时,我如何使用它将结果返回给我?控制台。Writeline似乎不起作用


  • 从另一个角度来看,您应该尝试使用blob上传的检查机制 在进展中,以下是一个示例:

     var speedSummary = blobService.createBlockBlobFromStream('mycontainer', file.name, fileStream, file.size, function(error, result, response) {...}
    
    setTimeout(function() {
        if (!finishedOrError) {
            var process = speedSummary.getCompletePercent();
            displayProcess(process);
            refreshProgress();
        }
    }, 200);
    
    您可以在此处找到更多详细信息:

    1-只能使用一个进度条


    2-您可以使用最近在github中发布的基于blob上载的进度机制。

    如果您已经接近,您需要在调用UploadFromStreamAsync之前向处理程序提供StorageProgress实例:

    long byteLength = 1048576L; // 1MB at time
    StorageProgress storageProgress = new StorageProgress(byteLength);
    progressHandler.Report(storageProgress);