public async Task <IActionResult> Create([Bind("Title,Upload,Photo,Id,Content,Text")] HomeSpecialityModern homeModern) { if (homeModern.Upload == null) { ModelState.AddModelError("Upload", "Şəkil məcburidir"); } else { if (homeModern.Upload.ContentType != "image/jpeg" && homeModern.Upload.ContentType != "image/png" && homeModern.Upload.ContentType != "image/gif") { ModelState.AddModelError("Upload", "Siz yalnız png,jpg və ya gif faylı yükləyə bilərsiniz"); } if (homeModern.Upload.Length > 1048576) { ModelState.AddModelError("Upload", "Fayl ölcüsu maximum 1MB ola bilər"); } } if (ModelState.IsValid) { var fileName = _fileManager.Upload(homeModern.Upload); homeModern.Photo = fileName; _context.Add(homeModern); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(homeModern)); }
public async Task <IActionResult> Edit(int id, [Bind("Title,Upload,Photo,Id,Content,Text")] HomeSpecialityModern homeModern) { if (id != homeModern.Id) { return(NotFound()); } if (ModelState.IsValid) { try { if (homeModern.Upload != null) { if (homeModern.Upload.ContentType != "image/jpeg" && homeModern.Upload.ContentType != "image/png" && homeModern.Upload.ContentType != "image/gif") { ModelState.AddModelError("Upload", "Siz yalnız png,jpg və ya gif faylı yükləyə bilərsiniz"); return(View(homeModern)); } if (homeModern.Upload.Length > 1048576) { ModelState.AddModelError("Upload", "Fayl ölcüsu maximum 1MB ola bilər"); return(View(homeModern)); } var oldFile = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads", homeModern.Photo); _fileManager.Delete(oldFile); var fileName = _fileManager.Upload(homeModern.Upload, "wwwroot/uploads"); homeModern.Photo = fileName; } _context.Update(homeModern); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!HomeModernExsist(homeModern.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(homeModern)); }