Click here to Skip to main content
14,579,188 members
Rate this:
Please Sign up or sign in to vote.
See more:
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;" />
Posted
Updated 28-Jun-20 5:30am
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   
Comments
[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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100