如何在简单网格中显示图像而不是从数据库显示网格。图像以字节数组格式保存

我正在使用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);

}


我希望网格显示包含其他列详细信息的图像。


慕标5832272
浏览 554回答 2
2回答

冉冉说

你可以<img>在td中创建标签并将base64字符串设置为src属性。<tbody>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @foreach (var item in entity.SaveImages)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>@item.Id</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>@item.ImageName</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><img src="'data:image/jpg;base64,' + @item.ImagePic" width="100" height="100" /></td>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tbody>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript