user1861857
user1861857

Reputation: 318

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?

Upvotes: 20

Views: 9279

Answers (1)

pfx
pfx

Reputation: 23224

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.

Upvotes: 15

Related Questions