public IActionResult CreatePhone(CreatePhoneViewModel phone) { if (ModelState.IsValid) { string uniqueFileName = null; if (phone.Photo != null) { string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + phone.Photo.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); phone.Photo.CopyTo(new FileStream(filePath, FileMode.Create)); } Phone newPhone = new Phone() { DisplayName = phone.DisplayName, Model = phone.Model, Price = (decimal)phone.Price, OldPrice = (decimal)phone.OldPrice, Color = phone.Color, Storage = phone.Storage, Quantity = phone.Quantity, Description = phone.Description, Manufacturer = phone.Manufacturer, PhotoPath = uniqueFileName }; _employeeRepository.AddPhone(newPhone); RedirectToAction("ListPhones"); } return(View(phone)); }
private string ProcessUploadedFile(CreatePhoneViewModel model) { string uniqueFileName = null; if (model.Photo != null) { string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "images"); 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); }