I am trying to upload a file and save into Azure Blob storage. The file is injected as a FormFile. The problem is that, there are errors when I convert the FormFile to a memory stream. The stream then uploads to Azure but with no data contained.
public async Task<IActionResult> Create([Bind("EndorsementId,FileName,ProviderId,Title")] Endorsement endorsement, IFormFile formFile)
{
if (ModelState.IsValid)
{
...
var data = new MemoryStream();
formFile.CopyTo(data);
var buf = new byte[data.Length];
data.Read(buf, 0, buf.Length);
UploadToAzure(data);
...
The errors are on the ReadTimeOut and WriteTimeOut properties of the memory stream. They say 'data.ReadTimeout' threw an exception of type 'System.InvalidOperationException' and 'data.WriteTimeout' threw an exception of type 'System.InvalidOperationException' respectively.
Here is how I injected the FormFile. There seems to be very little information on this. http://www.mikesdotnetting.com/article/288/uploading-files-with-asp-net-core-1-0-mvc
Thanks in advance.
IFormFile has CopyToAsync method for this purpose. You can just do something like below:
using (var outputStream = await blobReference.OpenWriteAsync())
{
await formFile.CopyToAsync(outputStream, cancellationToken);
}
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