0

I created an Ashx handler in C# that serves me up images based on a fileid parameter that gets passed on to me. I also have a simple tooltip preview script that I wrote, which is not working. You can see the image loading, but then after it loads, the image just vanishes.

I suspect the issue is in the ASHX handler because if I use a static image, it works just fine. Here is my ASHX handler code:

    public void ProcessRequest(HttpContext context)
    {
        string fileId = HttpUtility.UrlDecode(context.Request.QueryString["fileId"] ?? "") ?? "";
        string fullFileName = context.Server.MapPath("~/Uploads") + "\\" + fileId;

        using (FileStream s = File.Open(fullFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            context.Response.ContentType = HelperClasses.Utility.GetMimeTypeFromMagic(fullFileName);

            var buffer = new byte[s.Length];
            s.Read(buffer, 0, (int) s.Length);

            context.Response.BinaryWrite(buffer);
            context.Response.Write(buffer);

            s.Close();
        }
        context.Response.Flush();
        context.Response.Close();
    }

In addition, I've created a fiddle to demonstrate the issue.

Icemanind
  • 47,519
  • 50
  • 171
  • 296
  • hey i think its this issue with chrome, http://stackoverflow.com/questions/22219565/iis-chrome-failed-to-load-resource-neterr-incomplete-chunked-encoding – Mike Miller Aug 29 '14 at 17:01
  • Your handler is fine; if I plug the URL into a browser I get the image. – Slippery Pete Aug 29 '14 at 17:07
  • Does `GetMimeTypeFromMagic` (awesome method name btw) return the correct content type? – Arian Motamedi Aug 29 '14 at 17:08
  • It works fine in `IE`... – Arian Motamedi Aug 29 '14 at 17:10
  • In your handler, replace the line `context.Response.Close();` with `context.Response.End();` and see if this fixes the issue? – Jawwad Alam Aug 29 '14 at 17:11
  • 1
    @JawwadAlam -- That was the problem! It works great now! Make an answer out of your comment and I will accept it. – Icemanind Aug 29 '14 at 17:34
  • @PoweredByOrange - The files I have do not retain an extension, so I don't know what kind of file it is, so the `GetMimeTypeFromMagic` function will look at the header and the magic bytes to determine the file type (hence the name). I also have a sister function called `GetExtensionFromMagic` that does something similar, but returns the extension. – Icemanind Aug 29 '14 at 17:36

2 Answers2

0

You are throwing garbage at the end of the response, specifically because you are calling Response.Write in addition to BinaryWrite. If you look at the response of your handler, this is at the end (literally):

System.Byte[]

enter image description here

Obviously that isn't part of the image. This line should be removed:

context.Response.Write(buffer);

I would also avoid doing anything like Response.End and Response.Close. Let the ASP.NET runtime take care of that.

Better yet, if you are using the .NET Framework 4 or older, you can simplify the whole thing to this:

s.CopyTo(context.Response.OutputStream);
vcsjones
  • 138,677
  • 31
  • 291
  • 286
0

In your code, the line

context.Response.Close();

is the issue. The close method abruptly ends the response stream, see details here and also check this related question IIS & Chrome: failed to load resource: net::ERR_INCOMPLETE_CHUNKED_ENCODING

Replace the line with context.Response.End(); to normally end the response.

Community
  • 1
  • 1
Jawwad Alam
  • 322
  • 3
  • 7