public async Task <IActionResult> Add(AddAnnouncementViewData model) { if (ModelState.IsValid) { if (!HtmlHelpers.HtmlContainsText(model.Content)) { ModelState.AddModelError("Content", "Ogłoszenie musi zawierać treść."); } else { var files = new List <UploadedFile>(); if (model.Files != null) { foreach (var file in model.Files) { var ext = Path.GetExtension(file.FileName).ToLowerInvariant(); var fileName = Path.GetFileNameWithoutExtension(file.FileName); if (UploadedFile.PermittedExtensions.Contains(ext)) { using (var memoryStream = new MemoryStream()) { await file.CopyToAsync(memoryStream); if (memoryStream.Length < UploadedFile.MaxFileSize) { files.Add(new UploadedFile { FileName = fileName, FileExtension = ext, Content = memoryStream.ToArray() }); } else { ModelState.AddModelError("File", "Plik jest zbyt duży. " + UploadedFile.MaxFileSizeExceedeErrorMessage); } } } else { ModelState.AddModelError("Extension", "Niedozwolony typ pliku"); } } } if (ModelState.IsValid) { var announcement = new Announcement { Content = model.Content, EtoUserId = userManager.GetUserId(HttpContext.User) }; if (model.SelectedCourseID.HasValue) { announcement.CourseID = model.SelectedCourseID.Value; } var addedAnnouncement = announcementService.SaveAnnouncement(announcement); foreach (var file in files) { file.AnnouncementID = addedAnnouncement.AnnouncementID; announcementService.SaveUploadedFile(file); } } } } if (ModelState.IsValid) { TempData["Toast"] = Toasts.AnnouncementAdded; return(RedirectToAction("Index")); } else { var viewData = new AddAnnouncementViewData { UserCourses = coursesService.GetUserCourses(userManager.GetUserId(HttpContext.User)) }; return(View(viewData)); } }