Rahul

Rahul

  • 1.8k
  • 160
  • 1.6k

uploaded image not display in mvc

Sep 22 2020 7:20 AM
I am trying to display uploaded image in list page but not work
 
I know this is very basic thing but I am struggling with last two-three hour
 
HomeController.cs
  1. [HttpGet]    
  2. public ActionResult EmployeeCreate()    
  3. {    
  4.     return View();    
  5. }    
  6.   
  7. [HttpPost]    
  8. public ActionResult EmployeeCreate(emp emp, HttpPostedFileBase ePhoto)    
  9. {    
  10.     if (ePhoto != null && ePhoto.ContentLength > 0)    
  11.     {    
  12.         try    
  13.         {    
  14.             var fileName = Path.GetFileName(ePhoto.FileName);    
  15.             var rondom = Guid.NewGuid() + fileName;    
  16.             //image upload name + randomguid    
  17.             var path = Path.Combine(HttpContext.Server.MapPath("~/data/"), rondom);    
  18.             if (!Directory.Exists(HttpContext.Server.MapPath("~/data/")))    
  19.             {    
  20.                 Directory.CreateDirectory(HttpContext.Server.MapPath("~/data/"));    
  21.             }    
  22.             ePhoto.SaveAs(path);    
  23.             emp.ePhoto = path;    
  24.   
  25.             _dbfltEntities.emps.Add(emp);    
  26.             _dbfltEntities.SaveChanges();    
  27.             return RedirectToAction("EmployeeList");    
  28.         }    
  29.         catch(Exception ex)    
  30.         {    
  31.             throw ex;    
  32.         }    
  33.     }    
  34.     return View(emp);    
  35. }    
  36.   
  37. public ActionResult EmployeeList()    
  38. {    
  39.      return View(_dbfltEntities.emps.ToList());    
  40. }    
employeelist.cshtml
  1. @foreach (var item in Model)    
  2.   {    
  3.       <tr>    
  4.           <td>@item.empid</td>    
  5.           <td>    
  6.               @item.ename    
  7.           </td>    
  8.           <td>    
  9.               @item.edesignation    
  10.           </td>    
  11.           <td>    
  12.               @item.eexperience    
  13.           </td>    
  14.           <td>    
  15.               <img src="@Url.Content(@item.ePhoto)" style="width:50px;height:50px;" alt="Uploaded Image" />    
  16.               @item.ePhoto    
  17.           </td>  
when I copy the path and paste in new tab then show the image
 
 
but in list page not shown image
 
please help

Brought to you by:

Answers (7)