Difference between FileStreamResult and FilePathResult?

走远了吗. 提交于 2019-12-21 07:10:22

问题


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?


回答1:


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!



来源:https://stackoverflow.com/questions/863351/difference-between-filestreamresult-and-filepathresult

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!