2

For some strange reason, a five year old internal asp.net web app that is used through IE6 has suddenly developed an issue, even though there have been no code changes. Certain images that are being streamed back to the web browser aren't appearing for some users.

I don't know why this was suddenly started happening or what the cause is - however as part of the search, I'm considering if there's a flaw in the code used to stream back images.

The image is held in memory are a byte array, then streamed back using the following code. Is this the best way to stream an image back?

Response.Clear();
Response.ClearHeaders();
Response.ClearContent();        

// Work out file type
switch( Path.GetExtension( imageFilename ).ToLower() )
{
    case ".jpg":
    case ".jpeg":
        Response.ContentType = "image/jpeg";
        break;
    case ".gif":
        Response.ContentType = "image/gif";
        break;
    default:
        Response.ContentType = "binary/octet-stream";
        break;  
}

Response.AddHeader("Content-Length", imageBytes.Length.ToString() );
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite( imageBytes );
Response.End();

Instead of an image being shown in the page, a red cross is displayed. This only happens with the dynamically generated images shown on the page, rather than the statically linked images.

If I try and open the image in it's own window, I either see the image or a load of gobbledygook text (Which I'm guessing means the MIME type isn't being set/picked up by the browser)

6
  • At first glance that appears to be suitable, but could you outline what these 'issues' are that your clients are having? Is it all images or just some? Random? Do they download these images or are they embedded into a parent page? Commented Jun 6, 2011 at 12:44
  • When the images don't appear, what do you get in their place? Blank, Red-X, etc? Commented Jun 6, 2011 at 12:58
  • @Mantorok @Damien_The_Unbeliever I've updated the original question with clarifications. Commented Jun 6, 2011 at 13:56
  • I think Leon's answer could be correct, this rings a bell when I forgot to flush a response before closing it, you would only get half of the content, so it could very be that. Commented Jun 6, 2011 at 15:25
  • Our network has recently changed Anti-Virus software to Sophos 9.7, I'm wondering if this is connected... Commented Jun 7, 2011 at 8:54

2 Answers 2

1

Response.End() is generally bad as it aborts the IIS thread even if it's in the middle of Flush()-ing.

Use Response.Flush() followed by Response.Close() to make sure all content is sent to the client.

Sign up to request clarification or add additional context in comments.

2 Comments

I've updated the code to those Response.Flush(); Response.Close() instead of Response.End() - unfortunately it gives the same result
@Peter Check that IIS settings aren't changed - specifically I recall issues when using HTTP compression as that would break some of our streaming things.
0

I could be wrong about this one but i think the Content-Length should contain the length of the body in bytes. Response.BinaryWrite will base64 encode the data wich will be longer then the byte[] length you are telling it is in the header.

3 Comments

Hmm, given that only some clients are having the issue this seems unlikely, but your answer certainly raises a good question! I'm curious myself now.
.BinaryWrite is a wrapper for .OutputStream.Write, and can correctly be used to transmit binary data. It doesn't perform any encoding.
as @Damien_The_Unbeliever correctly pointed out the response won't be encoded. Therefore my whole answer is incorrect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.