Question

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?

Was it helpful?

Solution

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top