I have the following sample with two tasks. When the first is finished, the second should use the first's results. I am new in this field I I would appreciate if someone guide me how to chain it:
public async Task<string> UploadFile(string containerName, IFormFile file)
{
//string blobPath = "";
var container = GetContainer(containerName);
var fileName = file.FileName;
CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
using (var memoryStream = new MemoryStream())
{
// await file.CopyToAsync(memoryStream);
// await blob.UploadFromStreamAsync(memoryStream);
// upload only when the 'memoryStream' is ready
Task.Factory.StartNew(() => file.CopyToAsync(memoryStream))
.ContinueWith(m => blob.UploadFromStreamAsync(m.Result)); // ??
}
return blob.Uri.AbsoluteUri;
}
If not the second variant:
public string UploadFile(string containerName, IFormFile file)
{
var container = GetContainer(containerName);
var fileName = file.FileName;
CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
using (var memoryStream = new MemoryStream())
{
file.CopyToAsync(memoryStream).Wait();
blob.UploadFromStreamAsync(memoryStream).Wait();
//Task.Factory.StartNew(() => file.CopyToAsync(memoryStream))
// .ContinueWith(m => blob.UploadFromStreamAsync(m.Result)); // ??
}
return blob.Uri.AbsoluteUri;
}
Get the stream from the IFormFile and upload it directly
public async Task<string> UploadFile(string containerName, IFormFile file)
{
//string blobPath = "";
var container = GetContainer(containerName);
var fileName = file.FileName;
CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
await blob.UploadFromStreamAsync(file.OpenReadStream())
return blob.Uri.AbsoluteUri;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With