BinaryWrite or OutputStream.Write

  • Thread starter Thread starter PrePort
  • Start date Start date
P

PrePort

The goal here is to return a dynamic image from an HttpHandler that I have
written. 2 questions

1. I am reading the images into byte[] fine and cacheing them fine, but what
should I use to deliver the image to the client?
My code:

context.Response.ContentType = "image/gif";
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
context.Response.Cache.SetNoStore();
context.Response.Cache.SetExpires(DateTime.MinValue);
context.Response.BufferOutput = false;
context.Response.BinaryWrite(imageBytes);

or should the last line be:
context.Response.OutputStream.Write(imageBytes, 0, imageBytes.Length);

2. Should I do something like this:

context.Response.AddHeader("Content-Disposition","inline;filename=random.gif");
 
I wouldn't add the Content-Disposition header as this may prompt the
user to save or open the image file. You already set the ContentType to
an image type.

As for BinaryWrite or OutputStream.Write, I prefer BinaryWrite as a
short hand method. Under the covers, BinaryWrite most likely calls
Response.OutputSteam.Write
 
You were correct:

From Reflector:

public void BinaryWrite(byte[] buffer)
{
this.OutputStream.Write(buffer, 0, buffer.Length);
}
 
Back
Top