public static class FileUpload
{
[FunctionName("FileUpload")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, ILogger log)
{
string Connection = Environment.GetEnvironmentVariable("AzureWebJobsStorage");
string containerName = Environment.GetEnvironmentVariable("ContainerName");
Stream myBlob = new MemoryStream();
var file = req.Form.Files["File"];
myBlob = file.OpenReadStream();
var blobClient = new BlobContainerClient(Connection, containerName);
var blob = blobClient.GetBlobClient(file.FileName);
await blob.UploadAsync(myBlob);
return new OkObjectResult("file uploaded successfylly");
}
}
This is ok for uploading single file. What would be the best solution to provide multiple files upload using azure function.
2
Answers
This function expects the request body to be an array of objects, each containing a name property (the name of the file) and a stream property (a readable stream containing the contents of the file). You can modify the function to suit your specific needs.
I have reproduced in my environment and got expected result below:
Firstly, I have 3 files in my folder as below:
upload_ToBlob Method:
Now call the upload_ToBlob method using below code:
Output: