2

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 ?

Sam
  • 13,934
  • 26
  • 108
  • 194

2 Answers2

0

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://learn.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.

Cindy Pau
  • 13,085
  • 1
  • 15
  • 27
  • I guess you are right, my design is not very good. The idea was to have a a single upload point for all my webapps. I had not realized I could upload directly to storage from frontend (angular). However I'm not a fan of this approach since it involves extra security to handle. Instead I will upload to a secured webapi and call storage from there. So Front WevApp -> webapi WebApp -> Azure Storage. Would you agree with that? – Sam Apr 10 '20 at 07:28
  • 1
    @Sam This idea is ok. – Cindy Pau Apr 10 '20 at 07:30
0

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).

Gaspa79
  • 5,488
  • 4
  • 40
  • 63