0

i want to return an image in base64 from my controller to view using json.

public JsonResult changeProfile()
        {
            var userID = ((SessionModel)Session["SessionModel"]).UserID; // get current user id
            TBL_User item = _context.TBL_User.Find(userID);
            UserModel model = new UserModel();
            model.UserID = userID;
            model.BinaryPhoto = item.BinaryPhoto;

            return Json(new
            {
                ??????????????'
            },
                JsonRequestBehavior.AllowGet);
        }

what can i put there to return my image and display in the view? thanks

2
  • 1
    what is type of BinaryPhoto? Commented May 6, 2019 at 14:19
  • var base64 = Convert.ToBase64String(Model.BinaryPhoto); var imgsrc = string.Format("data:image/jpg;base64,{0}", base64);) @HienNguyen Commented May 6, 2019 at 14:21

1 Answer 1

0

Update controller

  public JsonResult changeProfile()
            {
                var userID = ((SessionModel)Session["SessionModel"]).UserID; // get current user id
                TBL_User item = _context.TBL_User.Find(userID);
                UserModel model = new UserModel();
                model.UserID = userID;
                model.BinaryPhoto = item.BinaryPhoto;

                var base64 = Convert.ToBase64String(model.BinaryPhoto); 
                var imgsrc = string.Format("data:image/jpg;base64,{0}", base64);

                return Json(new
                {
                    Image = imgsrc 
                },
                    JsonRequestBehavior.AllowGet);
            }

Update src for image in ajax success

$.ajax({
      url: "/changeProfile",  
      success: function(data) {
          $(".img-circle").attr('src', data.Image);
      }
   });
Sign up to request clarification or add additional context in comments.

2 Comments

<i class="fa fa-search text-symbol-color-do"></i> <img alt="" class="img-circle" src=""> what i put in src ?
change to $(".img-circle").attr('src', data.Image);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.