public IActionResult Create(EmployeeCreateViewModels model) { if (ModelState.IsValid) { string uniqueFileName = null; if (model.Photo != null) { uniqueFileName = NewFile(model); //string uploadsFolder = Path.Combine(_hostingEnvironment.WebRootPath, "images"); //uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Photo.FileName; //string filePath = Path.Combine(uploadsFolder, uniqueFileName); //model.Photo.CopyTo(new FileStream(filePath, FileMode.Create)); } Employee newEmployee = new Employee { Name = model.Name, Address = model.Address, Department = model.Department, Salary = model.Salary, PhotoPath = uniqueFileName }; _employeeRepository.AddNewEmployee(newEmployee); return(RedirectToAction("View", "Home", new { id = newEmployee.Id })); } return(View()); }
public string NewFile(EmployeeCreateViewModels model) { string uploadsFolder = Path.Combine(_hostingEnvironment.WebRootPath, "images"); string uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Photo.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { model.Photo.CopyTo(fileStream); } return(uniqueFileName); }
private string ProcessUploadFile(EmployeeCreateViewModels employee) { string uniqueFileName = null; if (employee.Image != null) { string uploadFolder = Path.Combine(env.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + employee.Image.FileName; string filePath = Path.Combine(uploadFolder, uniqueFileName); employee.Image.CopyTo(new FileStream(filePath, FileMode.Create)); } return(uniqueFileName); }