转换byte数组并显示图像中的Razor视图 [英] Convert Byte Array to Image and Display in Razor View
问题描述
我使用的 EF 4.1 code首先并为简单起见,假设我有以下的实体类:
I am using EF 4.1 Code First and for the sake of simplicity, let's say I have the following Entity class:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public Byte[] Image { get; set; }
}
我设法创建工作创建视图,允许增加一个Person对象的数据库中。
I have managed to create a working Create View that allows the Addition of a Person object into the Database.
但是,当我来显示一个人的细节,我 卡住上显示图像的。几个小时谷歌搜索后,我有以下几点:
But when I come to display the details for a Person, I get stuck on displaying the Image. After searching Google for many hours, I have the following:
// To convert the Byte Array to the author Image
public FileContentResult getImg(int id)
{
byte[] byteArray = DbContext.Persons.Find(id).Image;
return byteArray != null
? new FileContentResult(byteArray, "image/jpeg")
: null;
}
和在我试图名单中的人士细节来看,我有以下的,以获得要显示的图片:
And in the View where I am attempting to list the Person details, I have the following to get the Image to display:
<img src="@Html.Action("getImg", "Person", new { id = item.Id })" alt="Person Image" />
不过上面是的不工作,我的图像源的 [来源]属性返回空
我会 大大AP preciate一些帮助的以让我的图片来显示。
I would greatly appreciate some help with getting my images to display.
感谢你。
磁碗。
推荐答案
这样的:
<img src="@Url.Action("getImg", "Person", new { id = item.Id })" alt="Person Image" />
您需要 Url.Action
,而不是 Html.Action
,因为你只是想生成一个URL到 GETIMG
的行动。 Html.Action
做了完全不同。
You need Url.Action
and not Html.Action
because you just want to generate an url to the GetImg
action. Html.Action
does something entirely different.
这篇关于转换byte数组并显示图像中的Razor视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!