Guest User

Guest User

  • Tech Writer
  • 611
  • 129.4k

Upload different different image.

Mar 3 2020 12:06 AM
How too upload image different different in mvc single click.
  1. if (ModelState.IsValid)  
  2. {  
  3. if (Request.Files.Count > 0)  
  4. {  
  5. HttpPostedFileBase file = Request.Files[0];  
  6. if (file.ContentLength > 0)  
  7. {  
  8. var fileName = Path.GetFileName(file.FileName);  
  9. var supportedTypes = new[] { "jpg""jpeg""gif""x-png""png" };  
  10. var fileExt = System.IO.Path.GetExtension(fileName).Substring(1);  
  11. if (supportedTypes.Contains(fileExt))  
  12. {  
  13. obj_tblNews.NewsImage = Path.Combine(  
  14. Server.MapPath("~/Content/Upload"), fileName);  
  15. file.SaveAs(obj_tblNews.NewsImage);  
  16. obj_tblNews.NewsImage = file.FileName;  
  17. _db.tblNews.Add(obj_tblNews);  
  18. _db.SaveChanges();  
  19. ModelState.Clear();  
  20. }  
  21. //if(supportedTypes1.Contains(fileExt1))  
  22. //{  
  23. // obj_tblNews.BannerImage = Path.Combine(  
  24. // Server.MapPath("~/Content/Upload"), fileName1);  
  25. // file.SaveAs(obj_tblNews.BannerImage);  
  26. // obj_tblNews.BannerImage = file.FileName;  
  27. // _db.tblNews.Add(obj_tblNews);  
  28. // _db.SaveChanges();  
  29. // ModelState.Clear();  
  30. //}  
  31. }  
  32. }  
  33. }  
  34. }  
it's me code . comment code second image for . This code run successfull but store same image path in database.

Answers (1)