加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – FileStreamResult和FilePathResult之间的区别?

发布时间:2020-12-15 23:32:48 所属栏目:asp.Net 来源:网络整理
导读:我有一个简单的控制器返回图像: public class ImageController : Controller{ [AcceptVerbs(HttpVerbs.Get)] [OutputCache(CacheProfile = "StationeryImageCache")] public FileResult Show(int customerId,string imageName) { try { var path = string.C
我有一个简单的控制器返回图像:
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);
        }
    }
}

我的经理在代码审查期间注意到FileStreamResult,并提到我应该与:

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

这对我来说很有意义,所以我做到了.但几天之后,我们的其他开发者之一报告说,我正在返回的一些图像已经被破坏了.具体来说,有一些图像被切断了.图像的大小是正确的,但底部的25% – 40%的图像很简单.

当查看文件系统上的原始图像时,没有任何错误.我在浏览器中画了图像,看起来很好.但我的控制器只是返回部分图像.更糟糕的是,只有一些图像是问题…大约0个…虽然我无法找到工作的那些与那些没有任何区别.

在尝试调试此操作时,我将该操作的结果还原回FileStreamResult,并且突然一切都正在工作.

有谁知道这个解释吗?

解决方法

看来,在FilePathResult中使用的HttpResponse.TransmitFile有或已经有一些问题.这可能取决于您根据此 hotfix运行服务器的Windows版本.如果您在Google上搜索“response.TransmitFile错误”,您会收到很多错误.

我想你应该使用你的原始代码!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读