Reputation: 1
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.
my code is.. Controller
public ActionResult About()
{
var data = db.dbtable.ToList();
return View(data);
}
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;
}
}
on view page..
@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.
Thanks..
Upvotes: 0
Views: 3313
Reputation: 783
May make more sense just to keep the path the image.
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;
}
}
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");
}
}
for both
<tr>
<td>
<img src="/Home/GetImage/@item.RegNo" alt="" />
</td>
</tr>
Upvotes: 1