WebAPI support for File based Partial Content 206

Asp.net core support for partial content is far out easier than i imagine. For example, if your application delivering a huge file, 100 Megabytes for example. then you might want to request these data as chunks

Some notes : this only works for File result and not with JSON which might fetch some data from the table. For example, you have a huge json (about 2M) and defining range 30-1000 will not change returned data.

Sample controller to handle partial content :- Yeap this is all you need.

[HttpGet("largefile/{version}")]
public async Task<IActionResult> GetPartialPortfolios(string version, [FromQuery(Name = "last-modified")]DateTime? lastModified = null)
{
var path = Path.Combine("wwwroot", "sample.txt");
return File(System.IO.File.OpenRead(path), "text/plain");
}



Next, fire up your post and and you can see some of the results that i'm getting. Oh before anything else, please ensure you place a large txt file a folder.




As you can see from Postman. i create a request with Range : 30 - 1000 and the response is shown here too.

A couple of things to notice :-

Return result is 206. (Http Partial Content) 

Content-Length is 2500 ( which is the total file size we have) 



Comments

Popular posts from this blog

The specified initialization vector (IV) does not match the block size for this algorithm

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20