我正在将文件上传到 S3 。但是文件上传无声地失败而没有任何错误。所以我编写了一个事件处理程序来跟踪上传。
以下是我的文件上传功能。
我想等待事件处理程序,直到它完成执行。请注意它是同步文件上传。
public async Task<ActionResult> UploadBatchDocuments(UploadViewModel model)
{
TransferUtility transfer = new TransferUtility(client);
foreach (var file in model.Documents)
{
TransferUtilityUploadRequest request = new TransferUtilityUploadRequest()
{
BucketName = bucketName,
CannedACL = S3CannedACL.PublicRead,
Key = string.Format(file.FilePath + "/{0}", fileName),
InputStream = file.File.InputStream,
};
await Task.Run(()=>request.UploadProgressEvent += uploadRequest_UploadPartProgressEvent);
await Task.Run(() => transfer.Upload(request));
}
}
下面是我为每个文件上传运行的事件处理程序,
public async void uploadRequest_UploadPartProgressEvent(object sender, UploadProgressArgs e)
{
if (e.PercentDone == 100)
{
var subs = ((Amazon.S3.Transfer.TransferUtilityUploadRequest)sender).Key.Split('/');
var fileName = subs[subs.Length - 1];
for (int i = 0; i < tempModel.Documents.Count; i++)
{
if (tempModel.Documents[i].File.FileName.Equals(fileName))
{
tempModel.Documents[i].isUploaded = true;
}
}
}
}
我的问题是:控件不断在UploadBatchDocuments和uploadRequest_UploadPartProgressEvent(这是一个事件处理程序)之间切换。 必需:我希望控件在UploadBatchDocuments中等待等待线程在uploadRequest_UploadPartProgressEvent中完成