Truly unbuffered file uploads in ASP.NET Core?

Viewed 1110

The ASP.NET Core documentation on file uploading talks about the option to upload files in an unbuffered way using MultipartReader, however as I understand it, this only reads each MultipartSection in an unbuffered way - it has to read a whole MultipartSection before your code gets called with the whole section being buffered in memory (I have confirmed this by logging, and my logging only gets called once the HTTP request has finished posting). If you're uploading a large file as one MultipartSection this isn't terribly helpful.

Is there a way to do truly unbuffered uploading in ASP.NET Core (or at least, have the buffer be something small like 32kb)? As the data comes in from the client, it would be made available to my code to stream out to disk or uploaded somewhere else over a fast network connection?

1 Answers

it has to read a whole MultipartSection before your code gets called with the whole section being buffered in memory (I have confirmed this by logging, and my logging only gets called once the HTTP request has finished posting). If you're uploading a large file as one MultipartSection this isn't terribly helpful.

The code gets called once after HTTP request has finished posting. This is expected, but it doesn't means Asp.net core will buffer the data.

As the document mentioned that there are two general approaches for uploading files buffering and streaming in ASP.Net core. For the buffering approach, the entire file is read into an IFormFile. And for the streaming, there is no additional object created by the Asp.net core, it read the content directly from HttpContext.Request.Body, instead of created the IFormFile by Asp.NET Core.

And the goal of streaming approach is reducing the demands for memory or disk space when uploading files as it states below:

The file is received from a multipart request and directly processed or saved by the app. Streaming doesn't improve performance significantly. Streaming reduces the demands for memory or disk space when uploading files.

Related