Hello,
I uploaded image successfully, but now when I want to retrieve its not showing the image.
Kindly help.
Thanks, I tried the below and it worked fine.
<img src="data:image;base64,@System.Convert.ToBase64String(Model.UserImage)" style="width:100px; height:100px;" />
What I have tried:
For Upload my code is below:
if (image1 != null)
{
string path = Server.MapPath("~/App_Data/Images/Users/");
string imageName = Path.GetFileNameWithoutExtension(image1.FileName);
string imageExt = Path.GetExtension(image1.FileName);
string dt = DateTime.Now.ToString("yyyyMMddHHmmss");
string fpath = imageName + dt + imageExt;
string fullPath = Path.Combine(path, fpath);
image1.SaveAs(fullPath);
user.UserImage = new Byte[image1.ContentLength];
user.userImagePath = fullPath;
image1.InputStream.Read(user.UserImage, 0, image1.ContentLength);
db.Users.Add(user);
}
For Retrieving y controller code is:
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
User user = db.Users.Find(id);
if (user == null)
{
return HttpNotFound();
}
User u = new User();
u = db.Users.Where(x => x.UserId == id).FirstOrDefault();
ViewBag.UserImagePath = u.userImagePath;
return View(user);
}
And in View:
<img src="@Url.Content(ViewBag.UserImagePath)" style="width:200px; height:200px;" />