如何使用MVC在div中显示图像

时间:2014-07-01 12:32:15

标签: c# jquery asp.net-mvc asp.net-mvc-4

我使用以下控制器操作来获取Image From DB

控制器

        public ActionResult GetPhoto()
        {
            string base64String = string.Empty;
            byte[] bDoccontent;
            byte[] byteArray;
            byteArray = Encoding.UTF8.GetBytes(base64String);
            var photo = (from doc in db.Photos
                         where doc.CandidateID == "S1404234570"
                         select doc).FirstOrDefault();
            bDoccontent = photo.ImageFile;
            base64String = Convert.ToBase64String(bDoccontent, 0, bDoccontent.Length);
            byteArray = Encoding.UTF8.GetBytes(base64String);
            Stream obj = new MemoryStream(byteArray);
            StreamReader sr = new StreamReader(obj);
            byte[] buffer = Convert.FromBase64String(sr.ReadToEnd());
            return File(buffer, "image/jpeg");
        }

查看

    <div id="divUserImage">
    </div>

现在我需要在 divUserImage 中显示从Action返回的图像。如何做到这一点?

2 个答案:

答案 0 :(得分:2)

当您返回JPEG图片时,您可以将操作的网址放入src元素的img

<div id="divUserImage">
    <img src="@Url.Action("GetPhoto", "MyController")" />
</div>

答案 1 :(得分:1)

需要将该控制器调用为图像标记

<img src="@Url.Action("GetPhoto", "YourController")" alt="" />