public IActionResult Create(EmpoloyeeCreateViewModel model) { string uniqueFileName = processUpLoadedFile(model);; if (ModelState.IsValid) { //We need to Create Employee Object Out of model Employee newEmployee = new Employee() { Name = model.Name, Email = model.Email, Department = model.Department, Photopath = uniqueFileName }; _repos.Add(newEmployee); //Change the Create and Details View for handling photos remove noimage.jpgthere return(RedirectToAction("details", new { id = newEmployee.Id })); } else { return(View()); } }
//Created a seprate method for Handlig photo file upload used Interface for EmpoloyeeCreateViewModel //as the parent iNterface so both EmpoloyeeCreateViewModel and EmployeeEditViewModel use the same method private string processUpLoadedFile(EmpoloyeeCreateViewModel model) { string uniqueFileName = null; if (model.Photo != null) { ///To get the Phyiscal Path of the root folder we use IConfiguration /// need to inject IConfiguration here acessroot folder string upLoadsFolder = Path.Combine(env.WebRootPath, "images"); //We want the file Name to be Unique so we use GUID class uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Photo.FileName; //lets combine upLoadsFolder and uniqueFileName string filePath = Path.Combine(upLoadsFolder, uniqueFileName); //Added Usinf since the Filestream to be closed afetr Uploading fle using (var fileStream = new FileStream(filePath, FileMode.Create)) { model.Photo.CopyTo(fileStream); } } return(uniqueFileName); }