我正在使用ASP.NET MVC 4.0,我试图在一个简单的网格中显示图像,但网格显示为空白。图像以字节数组格式保存。我正在获取其他列详细信息但不是图像。下面是完整的控制器代码,用于以字节数组格式保存图像并从数据库显示: -public ActionResult Index(){ return View();}public ActionResult Savedata(HttpPostedFileBase Image){ SaveImage obj = new SaveImage(); string result; if (Image != null) { HttpPostedFileBase httpobj = Request.Files["Image"]; string[] Imagename = httpobj.FileName.Split('.'); obj.ImageName=Imagename[0]; using (Stream inputStream = Request.Files[0].InputStream) //File Stream which is Uploaded { MemoryStream memoryStream = inputStream as MemoryStream; if (memoryStream == null) { memoryStream = new MemoryStream(); inputStream.CopyTo(memoryStream); } obj.ImagePic = memoryStream.ToArray(); } var path = Path.GetFileNameWithoutExtension(Image.FileName) + DateTime.Now.ToString("ddMMyyhhmmss") + Path.GetExtension(Image.FileName); result = path.Replace(" ", string.Empty); var serversavepath = Path.Combine(Server.MapPath("~/DemoImages/") + result); Image.SaveAs(serversavepath); obj.ImageName= result; entity.SaveImages.Add(obj); entity.SaveChanges(); } //return View("Index"); return Content("<script>alert('Data Successfully Submitted');location.href='../Home/Index';</script>"); }public JsonResult BindGrid(){ DataRow[] result; var output = (from c in entity.SaveImages.AsEnumerable() select new { ID = c.Id, ImageName = c.ImageName, //ImagePic = c.ImagePic, ImagePic = Convert.ToBase64String(c.ImagePic) }).ToList(); var data = new { result = output }; return Json(output, JsonRequestBehavior.AllowGet);}我希望网格显示包含其他列详细信息的图像。
2 回答
冉冉说
TA贡献1877条经验 获得超1个赞
你可以<img>在td中创建标签并将base64字符串设置为src属性。
<tbody>
@foreach (var item in entity.SaveImages)
{
<tr>
<td>@item.Id</td>
<td>@item.ImageName</td>
<td><img src="'data:image/jpg;base64,' + @item.ImagePic" width="100" height="100" /></td>
</tr>
}
</tbody>
添加回答
举报
0/150
提交
取消