Error: Cannot convert from Microsoft.AspNetCore.Http.IFormFile to System.IO.Stream

Error: Cannot convert from Microsoft.AspNetCore.Http.IFormFile to System.IO.Stream

我正在尝试创建 Azure 函数,该函数从 html 表单 POST 请求中获取图像文件并将其保存到 Blob 存储以供其他函数进一步使用。这是我的代码:

public static class Function2
    {
        [FunctionName("Function2")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            foreach (var file in req.Form.Files)
            {
                using (var ms = new MemoryStream())
                {
                    var file2 = req.Form.Files[0];
                    await file2.CopyToAsync(ms);
                    ms.Seek(0, SeekOrigin.Begin);
                    var connectionString = "DefaultEndpointsProtocol=https;" +
                "AccountName=mystorageaccount;" +
                "AccountKey=8Hk5k6j65j5j665j67k==;" +
                "EndpointSuffix=core.windows.net";

                    // intialize BobClient 
                    Azure.Storage.Blobs.BlobClient blobClient = new Azure.Storage.Blobs.BlobClient(
                        connectionString: connectionString,
                        blobContainerName: "image-storage",
                        blobName: "images");

                    // upload the file
                    blobClient.Upload(file2);
                }
            }

            return new OkResult("Image uploaded successfully");
        }
    }

然而这引发了异常:

Error CS1503 Argument 1: cannot convert from 'Microsoft.AspNetCore.Http.IFormFile' to 'System.IO.Stream'

任何建议将不胜感激。

编辑:我之前使用 Azure 门户为我的存储帐户创建了 Blob 容器“图像存储”。

BlobCients Upload 方法需要 Stream 而不是 IFormFile.

传递 ms 变量中的 MemoryStream 将解决问题。

blobClient.Upload(ms);