Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
HTML
<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:
HTML
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:

HTML
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:
HTML
<img src="@Url.Content(ViewBag.UserImagePath)" style="width:200px; height:200px;" />
Posted
Updated 28-Jun-20 5:30am
v2

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
 
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


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