FileStreamResult和FilePathResult之间的区别? [英] Difference between FileStreamResult and FilePathResult?

查看:937
本文介绍了FileStreamResult和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);
        }
    }
}

我的经理code审查期间注意到FileStreamResult并提到我应该换吧:

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

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

这是有道理的我,所以我做到了。但过了几天我们的其他开发者的一个报道,有我在返回的图像是回来了损坏。具体来说,有很多的人在某个时候切断图像。的图像的大小是正确的,但最低25% - 40图像%被简单地去

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.

当在文件系统中的原始图像看没有什么不妥的地方。我在浏览器一屁股的形象和它看起来罚款。但我的控制器只返回图像的一部分。更糟的是,它只是为问题的一些图片...其中约30%......虽然我无法找到那些工作,那些不之间的任何特定的差异。

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.

在试图调试此我恢复了行动的结果返回给FileStreamResult,突然一切都工作了。

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

有谁知道这个解释?

推荐答案

看来是在FilePathResult使用的Htt presponse.TransmitFile有或有过一些问题。这可能取决于Windows版本你按照这个修补程序运行您的服务器。如果你在谷歌搜索的东西像'response.TransmitFile错误'你得到了很多错误。

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.

我猜你应该用你原来的code!

I guess you should use your original code!

这篇关于FileStreamResult和FilePathResult之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆