public RedirectToActionResult AddFile(IFormFile uploadedFile) { if (uploadedFile != null) { string namePhoto = ""; if (uploadedFile.FileName.Length <= 15) { namePhoto = uploadedFile.FileName; } else { var position = uploadedFile.FileName.IndexOf(".", StringComparison.Ordinal); namePhoto = uploadedFile.FileName.Substring(0, 11) + uploadedFile.FileName.Substring(position); } using (var fileStream = new FileStream(_appEnvironment.WebRootPath + @"\images\Gallery\" + namePhoto, FileMode.Create)) { uploadedFile.CopyTo(fileStream); } if (DashboardController.IdTour != 0) { TourPhoto file = new TourPhoto() { PhotoLink = namePhoto, TourTypeId = DashboardController.IdTour, ToutPhotoTypeId = _tourTypePhotoId }; _db.TourPhotos.Add(file); _db.SaveChanges(); } } return(RedirectToAction("Index")); }
public async Task <bool> PostPhoto(PostPhoto postPhoto) { var tour = await _dataContext.Tours.FirstOrDefaultAsync(x => x.Id == postPhoto.TourId); if (tour == null) { return(false); } var newEntity = new TourPhoto { TourId = tour.Id, Photo = await GetFileAsByteArrayAsync(postPhoto.TourPhoto), Tour = tour }; var tourPhoto = await _dataContext.TourPhotos.AddAsync(newEntity); await _dataContext.SaveChangesAsync(); return(true); }