Created
December 10, 2025 10:31
-
-
Save sunmeat/71011d661b680e68e0f5a7c2948743c9 to your computer and use it in GitHub Desktop.
підказочка для домашки з прикріпленням файлів у формі (ДЗ пошук фільмів)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // POST: Створення нового студента з можливістю завантажити кілька файлів | |
| [HttpPost] | |
| [ValidateAntiForgeryToken] | |
| public async Task<IActionResult> Create( | |
| [Bind("Name,Surname,Age,GPA")] Student student, | |
| List<IFormFile> files) // !!!!!!!!!! | |
| { | |
| if (ModelState.IsValid) | |
| { | |
| if (files?.Count > 0) | |
| { | |
| student.PhotoPaths ??= new List<string>(); | |
| var folder = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/uploads/students"); | |
| Directory.CreateDirectory(folder); | |
| foreach (var file in files.Where(f => f.Length > 0)) | |
| { | |
| var fileName = Guid.NewGuid() + Path.GetExtension(file.FileName); | |
| var filePath = Path.Combine(folder, fileName); | |
| await using var stream = new FileStream(filePath, FileMode.Create); | |
| await file.CopyToAsync(stream); | |
| student.PhotoPaths.Add("/uploads/students/" + fileName); | |
| } | |
| } | |
| _context.Add(student); | |
| await _context.SaveChangesAsync(); | |
| return RedirectToAction(nameof(Index)); | |
| } | |
| return View(student); | |
| } | |
| // POST: Редагування студента — оновлення даних + додавання/видалення фото | |
| [HttpPost] | |
| [ValidateAntiForgeryToken] | |
| public async Task<IActionResult> Edit( | |
| int id, | |
| [Bind("Id,Name,Surname,Age,GPA")] Student student, | |
| List<IFormFile> files, // !!!!!!!!!! | |
| string[]? deletePhotos = null) | |
| { | |
| if (id != student.Id) | |
| return NotFound(); | |
| if (ModelState.IsValid) | |
| { | |
| var existing = await _context.Students.FindAsync(id); | |
| if (existing == null) | |
| return NotFound(); | |
| // оновлюємо тільки ті поля, які дозволені через Bind | |
| existing.Name = student.Name; | |
| existing.Surname = student.Surname; | |
| existing.Age = student.Age; | |
| existing.GPA = student.GPA; | |
| // видаляємо вибрані старі фото | |
| if (deletePhotos?.Length > 0) | |
| { | |
| foreach (var path in deletePhotos) | |
| { | |
| if (existing.PhotoPaths.Remove(path)) | |
| { | |
| var fullPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", path.TrimStart('/')); | |
| if (System.IO.File.Exists(fullPath)) | |
| System.IO.File.Delete(fullPath); | |
| } | |
| } | |
| } | |
| // додаємо нові файли | |
| if (files?.Count > 0) | |
| { | |
| existing.PhotoPaths ??= new List<string>(); | |
| var folder = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/uploads/students"); | |
| Directory.CreateDirectory(folder); | |
| foreach (var file in files.Where(f => f.Length > 0)) | |
| { | |
| var fileName = Guid.NewGuid() + Path.GetExtension(file.FileName); | |
| var filePath = Path.Combine(folder, fileName); | |
| await using var stream = new FileStream(filePath, FileMode.Create); | |
| await file.CopyToAsync(stream); | |
| existing.PhotoPaths.Add("/uploads/students/" + fileName); | |
| } | |
| } | |
| await _context.SaveChangesAsync(); | |
| return RedirectToAction(nameof(Index)); | |
| } | |
| return View(student); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment