Home > .net 2.0 > streaming PDF to a client via C#

streaming PDF to a client via C#

July 31, 2006

I’ve battled with Response.WriteFile, Response.TransmitFile, and an array of other hacks the past week until I came across a forum article that defined just creating your OWN streamer for my PDFs.

In summary, create a bitwise connection to your file and stream it, 1k at a time, to the user. No fancy HTML streaming, just honest transmission from point-to-point. Slick.

Click More for the code.🙂

Context.Response.Buffer = false; 
System.IO.FileStream inStr = null; 
byte[] buffer = new byte[1024]; 
long byteCount;
inStr = System.IO.File.OpenRead(filename); 
while ((byteCount = inStr.Read(buffer, 0, buffer.Length)) > 0) 
{ 
    if (Context.Response.IsClientConnected) 
    { 
        Context.Response.OutputStream.Write(buffer, 0, buffer.Length); 
        Context.Response.Flush(); 
    } 
}
Categories: .net 2.0
%d bloggers like this: