HttpHandler to forward file with quick response

IDDaniel

New Member
I have files hosted on a UNC share that I want to forward through a web server to a client making an HTTP GET request. I've implemented an HttpHandler and it is working, except that the client is not receiving a timely response from the server. There is a significant wait time before the client receives the response, displays a save dialog, and downloads the file. It's almost as if the server is loading the entire file into the output stream before it is sending a single byte back to the client. This is a problem because it makes file downloads take longer than necessary, and clients could potentially time out before they get that first byte of a large file, if the UNC is slow. I want the web server to immediately send a response to the client as soon as it knows the file name and file size then start forwarding bytes as it downloads from the UNC share. Is this possible? Here are the two things I have tried so far:\[code\]public class DownloadHttpHandler : IHttpHandler{ public bool IsReusable { get { return true; } } public void ProcessRequest(HttpContext context) { context.Response.Clear(); context.Response.Buffer = false; context.Response.ContentType = "application/pdf"; var info = new FileInfo(@"\\uncshare\reallybigpdf.pdf"); context.Response.AddHeader("Content-Disposition", "attachment;filename=temp.pdf"); context.Response.AddHeader("Content-Length", info.Length.ToString()); context.Response.TransmitFile(@"\\uncshare\reallybigpdf.pdf"); context.Response.End(); }}public class DownloadHttpHandler : IHttpHandler{ public bool IsReusable { get { return true; } } public void ProcessRequest(HttpContext context) { var bytes = new byte[1024]; var byteCount = 1024; using (var stream = File.OpenRead(@"\\uncshare\reallybigpdf.pdf")) { context.Response.Clear(); context.Response.Buffer = false; context.Response.ContentType = "application/pdf"; context.Response.AddHeader("Content-Disposition", "attachment;filename=temp.pdf"); context.Response.AddHeader("Content-Length", stream.Length.ToString()); while (byteCount == 1024) { byteCount = stream.Read(bytes, 0, 1024); context.Response.OutputStream.Write(bytes, 0, byteCount); context.Response.OutputStream.Flush(); } context.Response.End(); } }}\[/code\]
 
Back
Top