views:

1621

answers:

2

I have a simple controller which returns images:

public class ImageController : Controller
{
    [AcceptVerbs(HttpVerbs.Get)]
    [OutputCache(CacheProfile = "StationeryImageCache")]
    public FileResult Show(int customerId, string imageName)
    {
        try
        {
            var path = string.Concat(Config.ImageDir, customerId, @"\", imageName);
            return new FileStreamResult(new FileStream(path, FileMode.Open), "image/jpeg");
        }
        catch(System.IO.FileNotFoundException ex)
        {
            throw new MissingImageException(imageName);
        }
    }
}

My manager noticed the FileStreamResult during a code review and mentioned I should swap it with:

return new FilePathResult(path, "image/jpeg");

This made sense to me so I did it. But after a few days one of our other devs reported that some of the images I was returning were coming back corrupted. Specifically, there were a lot of images that were cut off at some point. The size of the image was correct, but bottom 25% - 40% of the image was simply gone.

When looking at the original image on the file system there was nothing wrong with it. I plopped the image in a browser and it looked fine. But my controller was only returning part of the image. Worse, it was only some images that were issues... approximately %30 of them... though I'm unable to find any particular differences between those that work and those that don't.

While trying to debug this I reverted the action's result back to the FileStreamResult, and suddenly everything was working again.

Does anyone know an explanation for this?

+6  A: 

It appears that the HttpResponse.TransmitFile that is used in FilePathResult has or have had a few problems. It might depend on the version of Windows you are running your server according to this hotfix. If you search on Google for something like 'response.TransmitFile error' you get a lot of errors.

I guess you should use your original code!

HakonB
Nice find! I was about to start digging into the source but I wouldn't have noticed this even if I had.
Sailing Judo
A: 

I think you need a new manager.

Mike
Why? `return new FilePathResult(path, "image/jpeg");` is a lot cleaner and more simple than what he had. His manager suggested the proper thing. As HakonB stated, its a bug, not the manager's fault.
Baddie
-1 because this wasn't even an answer. Mike, judging by your reputation you are either new or you leave very unhelpful answers quite frequently. If you need to snipe somebody for some reason you would be best off leaving a comment. But really, helpful answers are far better.
Sailing Judo