展现在Asp.net-MVC3从数据库表图片 [英] Show images in table from database in Asp.net-mvc3

查看:113
本文介绍了展现在Asp.net-MVC3从数据库表图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含与图像场的用户信息,并在二进制数据存储图像的表

I have a table containing user information with image field and storing image in binary data.

我想告诉我的视图页面上这个图像与用户信息的表格。

I want to show this images on my view page in a table with user information.

我的code是..
控制器

my code is.. Controller

public ActionResult About()
        {
var data = db.dbtable.ToList();
return View(data);
}

获取图像的方法//章是DBTABLE对象

Get Image method // reg is object of dbtable

public FileContentResult GetImage(int id)
        {            
            reg = db.dbtable.FirstOrDefault(i => i.RegNo == id);            
            if (reg != null)
            {
                return File(reg.Photo, "image/jpeg");
            }
            else
            {
                return null;
            }
        }

查看页面上。

@model IEnumerable<MvcMySchool.dbtable>

<table width="50">
    <tr>
        <th> 
            Registration No.
        </th>
        <th>
            First Name
        </th>
        <th>
            Last Name
        </th>
        <th>
            City
        </th>
        <th>
            Photo
        </th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.RegNo)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FirstName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.LastName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.City)
            </td>
            <td>
            @*HttpContext.Current.Response.Write("<img width="75" height="75" src=@(Url.Action("GetImage", "Home", new { id = item.RegNo })) />")*@
                <img width="75" height="75" src=@(Url.Action("GetImage", "Home", new { id = item.RegNo })) />
            </td>
        </tr>
    }
</table>

有只显示后,第一行和没有在图像

It only shows the image in the first row and nothing after that.

谢谢..

推荐答案

可能更有意义,只是为了保持路径的形象。

May make more sense just to keep the path the image.

如果您filesteam,

If you filesteam,

public FileStreamResult GetImage(int id)
{

reg = db.dbtable.FirstOrDefault(i => i.RegNo == id);            
            if (reg != null)
            {

    MemoryStream ms = new MemoryStream(reg.Photo);
    return File(ms, "image/png");

            }
            else
            {
                return null;
            }
}

要保持更多的合乎逻辑的方式,ONY方式

more logical way to keep, ony way

public FileResult GetImage(int id)
{
reg = db.dbtable.FirstOrDefault(i => i.RegNo == id);            
                if (reg != null)
                {
    FileInfo fi = new FileInfo(Server.MapPath(reg.Photo));
    return File(fi.OpenRead, "image/png");
                }
}

<tr>
    <td>
       <img src="/Home/GetImage/@item.RegNo" alt="" />
    </td>
</tr>

这篇关于展现在Asp.net-MVC3从数据库表图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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