FileStreamResult在MVC3从流渲染图像时,显示为空文件浏览器 [英] FileStreamResult displays empty browser document when rendering image from stream in MVC3

查看:581
本文介绍了FileStreamResult在MVC3从流渲染图像时,显示为空文件浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在MVC3应用程序相当简单的操作应该渲染图像...

I have a fairly simple Action in an MVC3 application that should render an image...

  public FileStreamResult Photo(int id)
    {
        //get the raw bytes for the photo
        var qry = from p in db.Photos
                  where p.PhotoID == id
                  select p.PhotoData;
        var data = qry.FirstOrDefault();

        var mem = new MemoryStream(data);
        var fs = new  FileStreamResult(mem, "image/jpeg");
        return fs;
    }

当我运行此我得到在Chrome中一个空白文档,火狐显示的URL在实际的文档区域和IE渲染原始字节。

When i run this i get a blank document in Chrome, Firefox displays the URL in the actual document area and IE renders the raw bytes.

镀铬给我一个消息:资源间preTED为文档,但MIME类型的图像传输/ JPEG

Chrome gives me a message: Resource interpreted as Document but transferred with MIME type image/jpeg

这个建议,我认为数据流不被发送到浏览器,它实际上是在收到一个空文件,但IE建议相反。

This suggests to me that the stream data is not being sent to the browser and it is in fact receiving an empty document, but IE suggests the opposite.

任何人碰到这个来之前,或不知道如何解决它?

Anyone come across this before, or know how to get around it?

推荐答案

您不需要流,如果你已经有照片的字节数组:

You don't need a stream if you already have a byte array of the photo:

public ActionResult Photo(int id)
{
    var data = db.Photos.FirstOrDefault(p => p.PhotoID == id);
    if (data == null)
    {
        return HttpNotFound();
    }
    return File(data.PhotoData, "image/jpeg");
}

与code中的问题是,你需要重新设置之初内存流,但正如我说你不需要这一切。

The problem with your code is that you need to reset the memory stream at the beginning but as I said you don't need all this.

这篇关于FileStreamResult在MVC3从流渲染图像时,显示为空文件浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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