locked
The internals of using an HttpHandler and serving files RRS feed

  • Question

  • User-1293453231 posted
    I use an HttpHandler on my podcast site to catch the requests for MP3's and increment a download counter. This seems to work pretty well, and I haven't had any problems to date. It also helps force the "save as" dialog so people aren't viewing them in their browsers.

    However, when I try to apply the same concept to big video files, it chokes. iTunes says the network connection is reset while Firefox simply says the document contains no data.

    What might be going on here? The code is what you've likely seen in countless examples elsewhere, like this:

    public class MovHandler : IHttpHandler
    {
        public MovHandler()
        {
        }
       
        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.Clear();
            context.Response.ContentType = "video/quicktime";
            string path = context.Server.MapPath(context.Request.FilePath);
            NewsItem item = new NewsItem(Path.GetFileName(context.Request.FilePath));
            item.Downloads++;
            item.Update();
            context.Response.WriteFile(path);
            context.Response.End();
        }
    }

    Tuesday, November 1, 2005 5:36 PM

All replies

  • User-1514880523 posted
    When you transmit a file using WriteFile, I believe the file will actually be read into memory (this depends on the implementation of the HttpWorkerRequest that is used - IIS should use the ISAPIWorkerRequest which does read the file into memory first). When writing large files, this may be a problem (I guess an OutOfMemoryException may have been thrown without you noticing it).

    What is the size of the movie that you are testing with?

    If it indeed appears to be an issue with the file size, I suppose you should try to stream the file yourself (open a file stream and directly write to the HTTP output stream).

    Tuesday, November 1, 2005 6:15 PM
  • User-1293453231 posted
    The files are generally under 100 MB. On my blog, someone suggested using TransmitFile() instead of WriteFile().
    Tuesday, November 1, 2005 6:53 PM