Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why shouldnt I use IFormFile for large uploads

Using .net core 2.1

As per Microsoft documentation , IFormFile should be used to deal with small uploaded files, whereas MultipartReader should be used for larger files. Fair enough.

However, they also say

Files uploaded using the IFormFile technique are buffered in memory or on disk on the web server before being processed. Inside the action method, the IFormFile contents are accessible as a stream.

So if large files are sent to disk, and read with a stream, why shouldnt we always use IFormFile? I don’t get the reason why MultipartReader if larger files are not completly loaded in RAM anyway.

Could anyone explain what I’m missing here?

like image 470
user1861857 Avatar asked Dec 12 '18 15:12

user1861857


People also ask

How do I handle large uploads?

Possible solutions: 1) Configure maximum upload file size and memory limits for your server. 2) Upload large files in chunks. 3) Apply resumable file uploads. Chunking is the most commonly used method to avoid errors and increase speed.

How do I upload files to IFormFile?

Upload Single FileTo add view, right click on action method and click on add view. Then select View from left side filter and select Razor View – Empty. Then click on Add button. Create design for your view as per your requirements.

What is IFormFile C#?

IFormFile. ASP.NET Core has introduced an IFormFile interface that represents transmitted files in an HTTP request. The interface gives us access to metadata like ContentDisposition , ContentType , FileName and more. The IFormFile interface also allows us to read the contents of a file via an accessible Stream .


1 Answers

The IFormFile setup uses a buffering approach, consuming either disk space or memory.

Both disk and memory are resources that can come under pressure if the size or frequency of file uploads is to high, causing out of disk space or out of memory problems, which can make your site crash.

See the note on that same page.

Any single buffered file exceeding 64KB will be moved from RAM to a temp file on disk on the server. The resources (disk, RAM) used by file uploads depend on the number and size of concurrent file uploads. Streaming isn't so much about perf, it's about scale. If you try to buffer too many uploads, your site will crash when it runs out of memory or disk space.


A streaming approach via MultipartReader doesn't load the full file into memory and doesn't consume any disk space.

like image 188
pfx Avatar answered Sep 21 '22 08:09

pfx