I have a WCF service that can return a stream through WebGet. It works great. But I would like to implement Range header support, so only parts of the file are returned. This is my code:
public System.IO.Stream GetStream(string mElementID)
{
FileInfo file = GetFile(mElementID);
try
{
FileStream videoStream = File.OpenRead(file.FullName);
if (request.Headers.AllKeys.Contains("Range"))
{
long startRange = ...;
long endRange = ...;
videoStream.Position = startRange;
}
WebOperationContext.Current.OutgoingResponse.ContentType = GetMimeType(file);
WebOperationContext.Current.OutgoingResponse.Headers.Add("Accept-Ranges", "bytes");
return videoStream;
}
catch (FileNotFoundException){}
catch (IOException ex)
{
throw ex;
}
throw new WebFaultException(System.Net.HttpStatusCode.NotFound);
}
I just create a FileStream and return it. Now I wonder what is the best way to get the range of this stream.
I think I could set videoStream.Position to the initial value of Range, but what is the best way to get the part from something in the file somewhere in the file?
Should I create a MemoryStream and write the corresponding bytes to it? The files that are broadcast here are video files, so they can be quite large.