C# .NET Core 2.0 MVC文件上载进度
因此,我花了很长时间试图为我的.Net核心MVC应用程序显示进度条,而官方文档并没有太大帮助 此处的文档: 我还想在文件到达我的控制器时将其上载到Azure blob存储。 用户可以上传任意数量的文件 这是我上传的代码: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[]
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;
}
我想知道的是:
从另一个角度来看,您应该尝试使用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);