How to Post a File to an Azure Function in 3 Minutes
RSS feed
Date: Nov 10, 2020
Tags: .net azure
Share: Share on Twitter
Azure Functions are the best part of Azure (There, I said it!). With them, you can offload processing, unify application design, centralize functionality, and just do cool stuff. Recently, I needed to upload a file to Azure Functions, and found a hard time finding a blog on the easiest/fastest way to do it. So, I decided to write this quick article to show you how. And I think I can do it under 3 minutes!

Create a function


The first step of the process was I needed a function. For my scenario, I created a basic C# function in Visual Studio 2019. 

The VS template creates a base project with some sample code.

I was at less than a minute, which was good start. I have a lot VS add-ins, so opening the IDE took some of my time.

Add some code


Next, I added some code to process a posted file. There’s LOTS of different ways to go about this, so there’s not a “best” way here. In my case, I opted to pass the file using a form. I also renamed my file to FileProcessFunction.

There was very little code to use, as I’m just checking the uploaded file to make sure it was received. Here’s the code.

        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
                var formdata = await req.ReadFormAsync();
                var file = req.Form.Files["file"];
                return new OkObjectResult(file.FileName + " - " + file.Length.ToString());
            catch (Exception ex)
                return new BadRequestObjectResult(ex);

That’s the whole code! Exciting, I know. To confirm I actually received the file, I responded with the file name and size, for awesome screens shots for this blog.

Publish to Azure


The last step before testing was to deploy the code to Azure. I'll admit, I did create my Function App ahead of time. I could have created it from within VS 2019, but there’s plenty blogs on how to do that. I could have also just run the function locally, but there's no challenge in that.

Test It


After that arduous amount of coding, I was ready to test. I fired up Postman and created a new request. I selected POST and supplied my test file in the Body/form-data section. Here are the results.

And in the Azure Portal I opened the Function/Code + Test/Logs utility to see the real-time execution.

So, 2:42!  Not bad, if I must say so. There were a few places in the process I could trim even more time, if I ever encounter a “You MUST deploy this Function App in the next 2 minutes or Nakatomi Plaza is going to explode!!!” situation. But in the meantime, I’m happy with the results.

More importantly, I showed you how to use the Body/Form-Data content to pass files directly to your Azure Functions. There are many ways you can work with files, but this seemed to be one of the fastest. If you’re working with larger files, I highly recommend leveraging the Azure Blob Storage triggers for Functions, to save you some coding. 

Good luck!

Quick Links

Azure Functions Documentation


Die Hard