Mvc: Serving media with byte ranges

Created on 8 Dec 2015  路  13Comments  路  Source: aspnet/Mvc

How do we go about serving files in MVC 6 with byte range request support too (for serving videos for example so the requester can drag and load a video back and forward at any point)?

3 - Done enhancement

Most helpful comment

@muqeet-khan not currently committed, but we'll certainly consider it!

All 13 comments

@Tratcher can you confirm that byte ranges are supported by the static file middleware? For media scenarios is the support in static file middleware sufficient?

We had byte range support in Web API, but we haven't added it to MVC 6.

Yes, range requests are automatically supported in the static file middleware. Note that it only supports one range at a time.

I assume you're requesting this for the FileResult type? https://github.com/aspnet/Mvc/blob/31e42ee31286fe115c75336343a6cef68729f4d7/src/Microsoft.AspNet.Mvc.Core/FileResult.cs

It's mainly to serve videos in MVC.

The MVC 6 Response.Headers doesn't have a .Range property.

Is there a way to do it in MVC so I could have something like:

public ??? GetFile(string name)
{
... FileStream based on input name, and range from headers...
}

Not sure what type to return in Mvc6 or how to get the Range from the Request

Oh and it also needs to run in dnx50 and dnx451 as I run it on a mono ARM platform too.

@angelsix Why are you having MVC serve the file rather than having StaticFiles do it?

I'm unsure how to use StaticFiles in any functional way. I thought it just served files directly based on direct URL names. How could I use StaticFiles to serve a file say at /media/somefolder/movie.mp4 from http://localhost/get/mymovie ?

Ah, so there is some indirection going on. I think FileStreamResult or FileContentResult is what you're looking for, they just don't have range support yet.

Ok. Any plans for it any time soon or any way to implement it manually?
So FileStreamResult will return correctly as a stream so something requesting a 2GB video wont wait for the full 2GB to be returned and instead get streamed the content from start to finish?

@danroth27 with have to comment on plans.

To do it manually you can include the Microsoft.AspNet.Http.Extensions package and use httpContext.Request.GetTypedHeaders().Range to read the requested range. If it's a small range you can read it into a byte[] and use FileContentResult. If it's a wildcard range (30-*) you can Seek in the stream and use then FileStreamResult to send the remainder. If it's a large fixed range (0-1000000), then you need a custom stream or copy operation to stop in the right place.

Moving to backlog, no plans to implement this for the current version.

Is adding byte range support for filestreamresult\filecontent on the radar, now that 1.0 has been released?

@muqeet-khan not currently committed, but we'll certainly consider it!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

angelsix picture angelsix  路  61Comments

xaviergxf picture xaviergxf  路  35Comments

dotnetjunkie picture dotnetjunkie  路  43Comments

pranavkm picture pranavkm  路  35Comments

janpieterz picture janpieterz  路  43Comments