private string ProccedFileUpload(CreateGalleryPhoto model) { string uniquefilename = null; if (model.Photo != null && model.Photo.Length > 0) { var uploadFolder = Path.Combine(hostingEnvironment.WebRootPath, "images"); uniquefilename = Guid.NewGuid().ToString() + "_" + model.Photo.FileName; string filePath = Path.Combine(uploadFolder, uniquefilename); using (var filestream = new FileStream(filePath, FileMode.Create)) { model.Photo.CopyTo(filestream); } } return(uniquefilename); }
public async Task <IActionResult> Create(CreateGalleryPhoto model) { if (ModelState.IsValid) { string uniquefilename = null; if (model.Photo != null && model.Photo.Length > 0) { uniquefilename = ProccedFileUpload(model); } GalleryPhotos photo = new GalleryPhotos(); photo.Image = uniquefilename; _context.Add(photo); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(model)); }