Azure Function : 'Multipart body length limit 16384 exceeded.'

Viewed 1347

I want to upload a file from my web application to a HttpTriggered Azure Function. I have the following code :

     [FunctionName("UploadFileToStorage")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Function, "post", Route = "upload")] HttpRequest req
        , ILogger log)
      {            
         var formdata = await req.ReadFormAsync();
      }

This line of code that reads form data triggers the following exception : System.IO.InvalidDataException: 'Multipart body length limit 16384 exceeded.'

I cannot figure out how to allow bigger file to be uploaded. I know for asp.net core you can do this but what about Azure Functions ?

2 Answers

It is not recommended that you pass in a Form with a large file as an httptrigger request, which can easily cause a httptrigger timeout. This is a risky design,

Large, long-running functions can cause unexpected timeout issues.

Have a look of this:

https://docs.microsoft.com/en-us/azure/azure-functions/functions-best-practices#avoid-long-running-functions

Functions should be stateless and idempotent if possible. Associate any required state information with your data. For example, an order being processed would likely have an associated state member. A function could process an order based on that state while the function itself remains stateless.

you might be having a good Internet connection, but the users may not be. If you need to design this application, then its workflow should be like this: Web app → Azure Storage → Azure Functions. Use blobtrigger and then process the files uploaded to storage.

If you're not worried about timeouts and you're planning to upload like 50kb files or so, you can always add to your function the "RequestFormLimits" attribute, like this:

[FunctionName("UploadFileToStorage"), RequestFormLimits(ValueLengthLimit = 65536, MultipartBodyLengthLimit = 65536)]

You can go as high as you want (till int.MaxValue). However, if you are uploading files bigger than like a megabye, it'll be better to switch to another approach (filestream, upload to storage directly, etc).

Related