Raju Fodse

Raju Fodse

  • 1.4k
  • 244
  • 29.5k

Upload Default File Path if field is empty

Dec 6 2019 1:34 AM
I am using MVC. I have 'Contact' Table in which I have PhotoID filed. I want save data and if Phoid filed is empty then save default path like '~/Uploads/UploadImages/Guest.PNG' how can I do? my code is below  
 
  1. [HttpPost]  
  2.         [ValidateAntiForgeryToken]  
  3.         public ActionResult CreateContacts(Contact contacts, HttpPostedFileBase ImageFile)/*,TAG2,FLEX1,FLEX2,FLEX3*/  
  4.         {  
  5.                          
  6.             if (ModelState.IsValid)  
  7.             {  
  8.                 string fileName = Path.GetFileNameWithoutExtension(ImageFile.FileName);  
  9.                 string extension = Path.GetExtension(ImageFile.FileName);  
  10.                 fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;  
  11.                 contacts.PhotoID = "~/Uploads/UploadImages/" + fileName;  
  12.                 fileName = Path.Combine(Server.MapPath("~/Uploads/UploadImages/"), fileName);  
  13.                 ImageFile.SaveAs(fileName);  
  14.   
  15.                 using (ESContext db = new ESContext())  
  16.                 {  
  17.                     db.Contacts.Add(contacts);  
  18.                     db.SaveChanges();  
  19.                     return RedirectToAction("IndexContacts");  
  20.                 }  
  21.   
  22.                 }  
  23.            
  24.   
  25.             return View(contacts);  
  26.         }  
 

Answers (6)