WCF Service - Support for streaming files with Range: bytes support?

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)
{
        // build the filePath
        FileInfo file = GetFile(mElementID);
        try
        {
            FileStream videoStream = File.OpenRead(file.FullName);

            if (request.Headers.AllKeys.Contains("Range"))
            {
                long startRange = ...; // get the start range from the header
                long endRange = ...; // get the end range from the header
                videoStream.Position = startRange;
                // how can I set the end of the range?
                //TODO: Don't forget to add the Content-Range header to the response!
            }

            WebOperationContext.Current.OutgoingResponse.ContentType = GetMimeType(file);
            WebOperationContext.Current.OutgoingResponse.Headers.Add("Accept-Ranges", "bytes");
            return videoStream;
        }
        catch (FileNotFoundException){}
        catch (IOException ex)
        {
            throw ex;
        }
        // throw a 404
        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.

+5
1

, . . , . do

videoStream.Read(myByteArray, 0, myByteArray.Length)

, , .

, ( ), ( , ). .

+2

All Articles