Uploading file IFormFile to Azure C# function

Viewed 1201

I have this regular C# Azure function:

[FunctionName("Function1")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
    ILogger log) ...

And I need to upload here file, e.g. image. How can I add here IFormFile or is there other way to upload file to function?

1 Answers

To upload a file to an Azure Function, have a look at the Form of the incoming HttpRequest.

This works for me:

public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "files")] HttpRequest req,
    ILogger log)
{
    foreach(var file in req.Form.Files)
    {
        using (var ms = new MemoryStream())
        {
            var file = req.Form.Files[0];
            await file.CopyToAsync(ms);
            ms.Seek(0, SeekOrigin.Begin);
            // Do something with the file
        }
    }

    return new OkResult();
}
Related