[开始尝试将简单文件上传到.NET Core 3.0中的Azure存储Blob。我已经在VS中启动了一个全新的Razor Pages Web应用程序,并添加了Azure.Storage.Blobs
NuGet包(v12.4)。在blobClient.Upload(content)
中调用AzureBlobRepository.cs
时,代码只是挂起。它永远不会引发异常,也永远不会超时。这实际上是解决方案中的唯一代码(VS模板随附的样板代码除外)。
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();
}
}
复制到MemoryStream后,重置它的位置,否则BlobClient将尝试从末尾读取。
FormFile.CopyTo(memoryStream);
memoryStream.Position = 0;