public IActionResult Create(EmployeeCreateViewModel model) { if (ModelState.IsValid) { string uniqueFIleName = ProcessUploadedFile(model); #region code for multiple files upload //if (model.Photo != null && model.Photo.Count > 0) //{ // foreach (IFormFile photo in model.Photos) // { // string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "images"); // uniqueFIleName = Guid.NewGuid() + "_" + photo.FileName; // string filePath = Path.Combine(uploadsFolder, uniqueFIleName); // photo.CopyTo(new FileStream(filePath, FileMode.Create)); // } //} #endregion Employee newEmployee = new Employee { Name = model.Name, Email = model.Email, Department = model.Department, PhotoPath = uniqueFIleName }; _employeeRpository.Add(newEmployee); return(RedirectToAction("details", new { id = newEmployee.Id })); } return(View()); }
public IActionResult create(EmployeeCreateViewModel model) { if (ModelState.IsValid) { string uniqueFilename = ProcesUploadedFile(model); //string uniqueFilename = null; ////if(model.Photos !=null) ////{ //// string uploadsFolder = Path.Combine(hostingenvironment.WebRootPath, "images"); //// uniqueFilename = Guid.NewGuid().ToString() + "_" + model.Photos.FileName; //// string filePath = Path.Combine(uploadsFolder, uniqueFilename); //// model.Photos.CopyTo(new FileStream(filePath, FileMode.Create)); ////} //if (model.Photos != null && model.Photos.Count > 0) //{ // foreach (IFormFile photo in model.Photos) // { // string uploadsFolder = Path.Combine(hostingenvironment.WebRootPath, "images"); // uniqueFilename = Guid.NewGuid().ToString() + "_" + photo.FileName; // string filePath = Path.Combine(uploadsFolder, uniqueFilename); // photo.CopyTo(new FileStream(filePath, FileMode.Create)); // } // } Employee newEmployee = new Employee { Name = model.Name, Email = model.Email, Department = model.Department, // Store the file name in PhotoPath property of the employee object // which gets saved to the Employees database table PhotoPath = uniqueFilename }; _employeeRepository.Add(newEmployee); return(RedirectToAction("details", new { id = newEmployee.Id })); } return(View()); }