Click here to Skip to main content
15,792,510 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

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);
                    user.UserImage = new Byte[image1.ContentLength];
                    user.userImagePath = fullPath;
                    image1.InputStream.Read(user.UserImage, 0, image1.ContentLength);

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;" />
Updated 28-Jun-20 6:30am

1 solution

IIS keeps anything in app_data private and won't serve it to clients even if they request a valid file. If you want to link to images they have to be in a folder that is public, not app_data.
Share this answer
[no name] 29-Jun-20 2:29am    
Ok, Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900