Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to stream files in ASP.NET

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.

like image 824
Mun Avatar asked Mar 03 '09 22:03

Mun


People also ask

What is .NET stream file?

The FileStream is a class used for reading and writing files in C#. It is part of the System.IO namespace. To manipulate files using FileStream, you need to create an object of FileStream class. This object has four parameters; the Name of the File, FileMode, FileAccess, and FileShare.

What is a stream and types of streams in C#?

The stream is basically the sequence of bytes passing through the communication path. There are two main streams: the input stream and the output stream. The input stream is used for reading data from file (read operation) and the output stream is used for writing into the file (write operation).

What are files and streams in C# asp net?

File and stream I/O (input/output) refers to the transfer of data either to or from a storage medium. In . NET, the System.IO namespaces contain types that enable reading and writing, both synchronously and asynchronously, on data streams and files.

What is IO stream C#?

IO. Stream is an abstract class that provides standard methods to transfer bytes (read, write, etc.) to the source. It is like a wrapper class to transfer bytes. Classes that need to read/write bytes from a particular source must implement the Stream class.


2 Answers

I would take things outside of the "aspx" pipeline. In particular, I would write a ran handler (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;         }     } } 
like image 190
Marc Gravell Avatar answered Oct 23 '22 03:10

Marc Gravell


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.

like image 32
Robert Paulson Avatar answered Oct 23 '22 04:10

Robert Paulson