title: .aspx page Response.BinaryWrite image on IE7
body: <p>I maintain an application that has a .aspx page that loads on image from the database and uses Response.BinaryWrite() to write it back to the client.  This worked perfectly not long ago.  Two things have changed,  we upgraded the application to .NET 3.5 and they upgraded all the computers at work to IE7.</p>
  
  <p>Everything works fine on Firefox, but all I get in IE7 is a red X.  So I assume this issue is related to IE7?  Is there a security setting somewhere that would stop it from loading images from a .aspx form?  It's already set to display based on the content type and not the extension.</p>
  
  <p>Here is some of the code.  Like I said, I just maintain this app and didn't write it.  I know using Session is not a great way of doing it, but it's what I have and the switch statement is just a "wtf?".</p>
  
  <pre><code>&lt;asp:image id="imgContent" runat="server" Visible="true" ImageUrl="ProductContentFormImage.aspx"&gt;&lt;/asp:image&gt;
  
      protected void Page_Load(object sender, System.EventArgs e)
      {
          Hashtable hshContentBinary = (Hashtable)Session["hshContentBinary"];
          byte[] content = (byte[]) hshContentBinary["content"];
          string extension = (string) hshContentBinary["extension"];
  
  
          string contentTypePrefix = "application";
          switch(extension.ToLower())
          {
              case "gif":
              case "jpg":
              case "bmp":
                  contentTypePrefix = "image";
                  break;
              case "tif":
                  contentTypePrefix = "image";
                  break;
              case "tiff":
                  contentTypePrefix = "image";
                  break;
              case "eps":
                  contentTypePrefix = "image";
                  break;
              default:
                  Response.AppendHeader( 
                      "Content-disposition",
                      "attachment; filename=content." + extension );
                  break;
          }
          Response.ContentType = contentTypePrefix + "/" + extension;
          Response.BinaryWrite(content);
      }
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>OK, I followed your suggestions and through a little more research I have changed the method to the following, but it still doesn't work.</p>
  
  <pre><code>protected void Page_Load(object sender, System.EventArgs e)
  {
      Hashtable hshContentBinary = (Hashtable)Session["hshContentBinary"];
      byte[] content = (byte[]) hshContentBinary["content"];
      string extension = (string) hshContentBinary["extension"];
      string contentType;
      string contentDisposition = "inline; filename=content." + extension;
  
      Response.ClearContent();
      Response.ClearHeaders();
      Response.Clear();
  
      switch(extension.ToLower())
      {
          case "gif":
              contentType = "image/gif";
              break;
          case "jpg":
          case "jpe":
          case "jpeg":
              contentType = "image/jpeg";
              break;
          case "bmp":
              contentType = "image/bmp";
              break;
          case "tif":
          case "tiff":
              contentType = "image/tiff";
              break;
          case "eps":
              contentType = "application/postscript";
              break;
          default:
              contentDisposition = "attachment; filename=content." + extension;
              contentType = "application/" + extension.ToLower();
              break;
      }
  
      Response.Buffer = true;
      Response.Expires = 0;
      Response.ContentType = contentType;
      Response.AddHeader("Content-Length", content.Length.ToString());
      Response.AddHeader("Content-disposition", contentDisposition);
      Response.Cache.SetCacheability(HttpCacheability.NoCache);
      Response.BinaryWrite(content);
      Response.End();
  }
  </code></pre>