IFormFile performance considerations

bjsomworker

New member
Joined
Jul 28, 2023
Messages
2
Programming Experience
3-5
Hi guys,
I am currently working with large file uploads in my Rest API, and have ready several comments saying that IFormFile should not be used with large files. I have found that it seams to work rather fine with large uploads. In fact, in the documentation found in the link below it is expressed that for files larger than 84kb (default), the file will be saved temporarily to disc. This shows responsiveness to deal with large files, not storing them in memory. In fact, when testing large file uploads with IFormFile, sending a file over 500MB, memory usage only showed to increase 7-8MB, which clearly shows that the application responds with a good way to deal with large files. When trying to implement a MultipartReader (recommended as a solution when working with large file uploads), it appeared to be much worse both in time consumption and memory consumption than using IFormFile.

I am a bit unsure of this topic, and need you guys' input.
Microsoft should document this more clearly, and many comments I have seen does not prove their statement, so I highly appreciate all feedback from this community.

 
IFormFile is just an interface definition. It's the implementation of that interface that could be potentially a performance hog. Are you asking about the default implementation?
 
IFormFile is just an interface definition. It's the implementation of that interface that could be potentially a performance hog. Are you asking about the default implementation?

I am asking about how it is used as a parameter in an endpoint, and how the file contents of a request automatically maps into it. Here is the endpoint method, if that clears things up:


C#:
public async Task<IActionResult> PostAsync(string storageName, IFormFile fileToUpload)
        {
            if (fileToUpload == null || fileToUpload.Length == 0)
                throw new ArgumentException("A file is not send or empty. Make sure you include the file you want to upload as multipart content");

            var fileName = await _storageStrategy.UploadAsync(storageName, fileToUpload);

            var jsonObject = new JObject
            {
                {
                    "fileName", fileName
                }
            };

            return Ok(jsonObject);
        }
 
You can trace through the source code of .NET Core to try to find how the implementation works:
 

Latest posts

Back
Top Bottom