Reputation: 13931
Trying to get started uploading a simple file to an Azure Storage Blob in .NET Core 3.0. I've started a brand new Razor Pages Web Application in VS, added the Azure.Storage.Blobs
NuGet package (v12.4). When calling blobClient.Upload(content)
in AzureBlobRepository.cs
, the code just hangs. It never throws an exception and never times out. This is literally the only code in the solution (except for boiler plate code that comes with the VS template).
Index.cshtml
@page
@model IndexModel
<div class="text-center">
<form method="post" enctype="multipart/form-data">
<input type="file" asp-for="@Model.FormFile " />
<button asp-page-handler="Upload" type="submit">Submit</button>
</form>
</div>
Index.cshtml.cs
public class IndexModel : PageModel
{
public IFormFile FormFile { get; set; }
public IActionResult OnPostUpload()
{
using (var memoryStream = new MemoryStream())
{
FormFile.CopyTo(memoryStream);
var container = new BlobContainerClient("UseDevelopmentStorage=true", "testcontainer");
container.CreateIfNotExists();
BlobClient blobClient = container.GetBlobClient(FormFile.FileName);
blobClient.Upload(memoryStream); // CODE HANGS HERE FOREVER
}
return Page();
}
}
Upvotes: 2
Views: 1117
Reputation: 89141
Reset the Position of the MemoryStream after you copy to it, or else the BlobClient will try to read from the end.
FormFile.CopyTo(memoryStream);
memoryStream.Position = 0;
Upvotes: 5