Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between FileStreamResult and FilePathResult?

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?

like image 651
Sailing Judo Avatar asked May 14 '09 13:05

Sailing Judo


People also ask

What is a FileStreamResult?

FileStreamResult Sends binary content to the response by using a Stream instance when we want to return the file as a FileStream. public FileStreamResult CreateFile() { var stream = new MemoryStream(Encoding.ASCII.GetBytes("Hello World")); return new FileStreamResult(stream, new MediaTypeHeaderValue("text/plain"))

Does FileStreamResult dispose stream?

ASP.NET will handle getting rid of this stream for you once the transfer is complete (part of the FileStreamResult), so you don't need to worry about cleaning it up.

How many types of results are there in MVC?

The ASP.NET MVC framework supports several types of action results including: ViewResult - Represents HTML and markup. EmptyResult - Represents no result. RedirectResult - Represents a redirection to a new URL.

What is FileContentResult in C#?

FileContentResult(Byte[], String) Initializes a new instance of the FileContentResult class by using the specified file contents and content type.


1 Answers

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!

like image 185
HakonB Avatar answered Oct 15 '22 13:10

HakonB