private string ProcessUploadedFile(EmplyoeeCreateViewModel model) { string uniqueFileName = null; if (model.Photo != null) { string uplaodsFolder = Path.Combine(hostingEnvironment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Photo.FileName; string filePath = Path.Combine(uplaodsFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { model.Photo.CopyTo(fileStream); } } return(uniqueFileName); }
public IActionResult Create(EmplyoeeCreateViewModel model) { if (ModelState.IsValid) { string uniqueFileName = ProcessUploadedFile(model); 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()); }