public IActionResult ProductAdd(HomeProductAddViewModel model) { if (ModelState.IsValid) { 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); model.Photo.CopyTo(new FileStream(filePath, FileMode.Create)); } Product newProduct = new Product { Name = model.Name, Description = model.Description, Quantity = model.Quantity, Discount = model.Discount.ToString(), PhotoPath = uniqueFileName }; cartContentsRepo.Add(newProduct, dbContext, httpContext, httpClient); return(RedirectToAction("ProductDetails", new { id = newProduct.Id })); } return(View()); }
private string ProcessUploadedFile(HomeProductAddViewModel 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); }