显示数据库图像(字节[])在剃刀/ MVC3? [英] Displaying database image (bytes[]) in Razor/MVC3?

查看:134
本文介绍了显示数据库图像(字节[])在剃刀/ MVC3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我返回从我的MS SQL数据库2008R2的数据集,其中包含我已经从我的Razor视图获取数据的DataTable。我添加包含图像的缩略图,我试图显示在该视图一个byte []字段。

I am returning a dataset from my MS SQL 2008R2 database that contains a datatable that I am already getting data from on my Razor view. I added a byte[] field that contains an image thumbnail that I am trying to display on that view.

由于该字节数组是相对较小的,我想我会尝试显示字节数组内联。然而,阅读可能会有一些浏览器相关的问题后,我放弃了这一点。

Since the byte array is relatively tiny, I figured I would try displaying the byte array inline. However, after reading there may be some browser-related issues, I abandoned this.

创建一个控制器方法似乎要走的路,(这两种方法找到<一href=\"http://stackoverflow.com/questions/6040920/display-an-image-contained-in-a-byte-with-asp-net-mvc3\">here),不过我已经有字节数组准备去我的看法,并且不需要再拍分贝呼吁得到它基于一个ID。

Creating a controller method seemed the way to go, (both methods found here), however I already have the byte array ready to go on my view, and don't need to make another db call to get it based on an ID.

这样做,并没有显而易见的原因工作:

Doing this, doesn't work for obvious reasons:

......在页面上显示的其他数据....

... displaying other data on the page ....

@if (Model.dsResults.Tables[0].Rows[i]["ImageBytes"] == null)
{
    <img src="@Url.Content("~/Content/Images/NoPhoto.png")" border="0" />
}
else
{
    <img src="@Url.Action("GetImage", "SearchResults", new { imageBytes = Model.dsResults.Tables[0].Rows[i]["ImageBytes"] })" alt="Product Image" />
}

...

控制器方法:

[Authorize]
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult GetImage(byte[] imageBytes)
{
    byte[] byteArray = imageBytes;
    return new FileContentResult(byteArray, "image/jpeg");
}

...因为这基本上是试图发送通过HTTP字节数组。

... as this essentially is attempting to send the byte array over http.

所以,我的问题是,既然我已经在我的Razor视图的字节数组,我怎么显示呢?

So, my question is, since I already have the byte array on my Razor view, how do I display it?

- 更新 -

我认识到,在视图中做处理不推荐,但由于该数据已经存在,不能像这样做:

I realize that doing processing in the view isn't recommended, but since the data is already there, can't something like this be done:

Response.Write((byte[])Model.dsResults.Tables[0].Rows[i]["ImageBytes"]);

或者...

Stream s = new MemoryStream(((byte[])Model.dsResults.Tables[0].Rows[i]["ImageBytes"]));
System.Drawing.Image img = new System.Drawing.Bitmap(s);

在其他的贴子我读过你一个做的Response.Write(字节[] ...)),但这并不在这种情况下工作。

In other postings I've read that you an do a Response.Write(byte[]...)) but this doesn't work in this case.

- UPADATE -

在我的效率正在进行搜索(不必再拍请求DB)的<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.helpers.webimage%28v=vs.99%29.aspx\">WebImage助手似乎是一个不错的人选。它的一个构造函数将接受一个字节数组初始化类,然后使用.WRITE(JPEG)的方法,我可以看到图像。

In my ongoing search for efficiency (not having to make another request to the db) the WebImage helper seems to be a good candidate. One of its constructors will accept a byte array to initialize the class, then using the .Write("jpeg") method, I can see the image.

<td>
    @{
        WebImage webImage = new WebImage(((byte[])Model.dsResults.Tables[0].Rows[i]["ImageBytes"]));
        webImage.Write("jpeg");
    }
</td>

使用WebImage.Write()是一旦用的图像是呈现在页面上的唯一问题。有没有一种方法来呈现这个直接到的Razor视图页面?

The problem with using WebImage.Write() is that once used, the image is the only thing that renders on the page. Is there a way to render this directly to a "control" on a Razor view page?

- 更新 -

这将继续来烦我......所以我尝试以下,这我想可能工作,因为这是我们从操作做什么...

This continues to bug me... so I tried the following, which I figured may work, since this is what we're doing from the Action...

if (Model.dsResults.Tables[0].Rows[i]["ImageBytes"] != DBNull.Value)
{
    var img1 = new FileContentResult(((byte[])Model.dsResults.Tables[0].Rows[i]["ImageBytes"]), "image/jpeg");
    <text>
        <img src="@new FileContentResult(((byte[])Model.dsResults.Tables[0].Rows[i]["ImageBytes"]), "image/jpeg")" />
    </text>
}

...不能正常工作。

... doesn't work.

推荐答案

最后,我去了两趟的路线。非常低效,作为图像字节已经准备好了的视图。应该有一个网络辅助图像控制,将直接从图像的字节数组(或图像类型)渲染图像。也许有,我错过了。

Eventually, I went for the two trips route. Highly inefficient, as the image bytes were already ready to go on the view. There should be a web helper image control that will render the image directly from the image's byte array (or image type). Maybe there is and I missed it.

要获得字节(是的,从头再来):

To get the bytes (yes, all over again):

    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult GetThumbnailImage(string itemListID)
    {
        byte[] imageBytes = null;

        client = new FeederServiceClient();
        imageBytes = client.GetItemThumbnail( itemListID );

        if (imageBytes == null)
        {
            return new FilePathResult("~/Content/Images/NoPhoto.png", "image/png");
        }
        else
        {
            return new FileContentResult(imageBytes, "image/jpeg");
        }
    }

然后显示图像:

<img src="@Url.Content("~/Thumbnails/" + @Model.dsResults.Tables[0].Rows[i]["ItemListID"] )" />

通过放置在Global.asax中以下路径:

With the following route placed in Global.asax:

routes.MapRoute(name: "Thumbnails", url: "Thumbnails/{itemListID}", defaults: new { controller = "Results", action = "GetThumbnailImage" });

这篇关于显示数据库图像(字节[])在剃刀/ MVC3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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