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