views:

7507

answers:

4

What's the best way to stream files using ASP.NET?

There appear to be various methods for this, and I'm currently using the Response.TransmitFile() method inside an http handler, which sends the file to the browser directly. This is used for various things, including sending FLV's from outside the webroot to an embedded Flash video player.

However, this doesn't seem like a reliable method. In particular, there's a strange problem with Internet Explorer (7), where the browser just hangs after a video or two are viewed. Clicking on any links, etc have no effect, and the only way to get things working again on the site is to close down the browser and re-open it.

This also occurs in other browsers, but much less frequently. Based on some basic testing, I suspect this is something to do with the way files are being streamed... perhaps the connection isn't being closed properly, or something along those lines.

After trying a few different things, I've found that the following method works for me:

Response.WriteFile(path);
Response.Flush();
Response.Close();
Response.End();

This gets around the problem mentioned above, and viewing videos no longer causes Internet Explorer to hang.

However, my understanding is that Response.WriteFile() loads the file into memory first, and given that some files being streamed could potentially be quite large, this doesn't seem like an ideal solution.

I'm interested in hearing how other developers are streaming large files in ASP.NET, and in particular, streaming FLV video files.

+1  A: 

Try opening the file as a stream, then using Response.OutputStream.Write(). For example:

Edit: My bad, I forgot that Write takes a byte buffer. Fixed

byte [] buffer = new byte[1<<16] // 64kb
int bytesRead = 0;
using(var file = File.Open(path))
{
   while((bytesRead = file.Read(buffer, 0, buffer.Length)) != 0)
   {
        Response.OutputStream.Write(buffer, 0, bytesRead);
   }
}
Response.Flush();
Response.Close();
Response.End();

Edit 2: Did you try this? It should work.

Randolpho
Thanks, but I don't think that works... Response.OutputStream.Write doesn't take a Stream parameter (not in ASP.NET 2.0 anyway), only a byte array, which would require the file to be loaded into memory first, and therefore not really better than the WriteFile solution.
Mun
Yeah, I wasn't paying attention when I wrote the code. Fixed
Randolpho
Regarding the byte array -- you don't have to load the entire stream into memory. The code I have uses at most 64kb of memory (plus minor overhead) during streaming.
Randolpho
+3  A: 

I would take things outside of the "aspx" pipeline. In particular, I would write a ran hanlder (ashx, or mapped via config), that does the minimum work, and simply writes to the response in chunks. The handler would accept input from the query-string/form as normal, locate the object to stream, and stream the data (using a moderately sized local buffer in a loop). A simple (incomplete) example shown below:

public void ProcessRequest(HttpContext context) {
    // read input etx
    context.Response.Buffer = false;
    context.Response.ContentType = "text/plain";
    string path = @"c:\somefile.txt";
    FileInfo file = new FileInfo(path);
    int len = (int)file.Length, bytes;
    context.Response.AppendHeader("content-length", len.ToString());
    byte[] buffer = new byte[1024];
    Stream outStream = context.Response.OutputStream;
    using(Stream stream = File.OpenRead(path)) {
        while (len > 0 && (bytes =
            stream.Read(buffer, 0, buffer.Length)) > 0)
        {
            outStream.Write(buffer, 0, bytes);
            len -= bytes;
        }
    }
}
Marc Gravell
I'd love to know why the downvote... I won't be offended... speak up!
Marc Gravell
A downvote without a reason doesn't help anybody. I thought that your answer was good. +1 to bring you back to parity.
Guy
I'm not the one who downvoted, but thought I'd point out that the initial question does actually state the code is being used in an http handler... Thanks for the suggestion anyway though.
Mun
+3  A: 

Take a look at the following article Tracking and Resuming Large File Downloads in ASP.NET which will give you more in depth than just open a stream and chuck out all the bits.

The http protocol supports ranged byte requests and resumeable downloads, and many streaming clients (like video players or Adobe pdf) can and will try to chunk these up, saving bandwidth and giving your users a better experience.

Not trivial, but it's time well spent.

Robert Paulson
That's a very interesting article, which I have now bookmarked. +1
Randolpho
A: 

After trying lots of different combinations, including the code posted in the various answers, it seems like setting Response.Buffer = true before calling TransmitFile did the trick and the web application is now a lot more responsive in Internet Explorer.

In this particular case, the SWF extension is also mapped to ASP.NET, and we're using a custom handler in our web application to read the files from disk and then send them to the browser using Response.TransmitFile(). We've got a flash-based video player to play video files which are also SWF's, and I think having all of this activity go through the handler without buffering is what may have been causing strange things to happen in IE.

Mun