public async Task <IActionResult> Create([Bind("Name,Upload,Photo,Id")] CategoryCarusel brandCarusel) { if (brandCarusel.Upload == null) { ModelState.AddModelError("Upload", "The Photo field is required."); } else { if (brandCarusel.Upload.ContentType != "image/jpeg" && brandCarusel.Upload.ContentType != "image/png" && brandCarusel.Upload.ContentType != "image/gif") { ModelState.AddModelError("Upload", "You can only download png, jpg or gif file"); } if (brandCarusel.Upload.Length > 1048576) { ModelState.AddModelError("Upload", "The file size can be a maximum of 1 MB"); } } if (ModelState.IsValid) { var fileName = _fileManager.Upload(brandCarusel.Upload); brandCarusel.Photo = fileName; _context.Add(brandCarusel); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(brandCarusel)); }
public async Task <IActionResult> Edit(int id, [Bind("Name,Upload,Photo,Id")] CategoryCarusel brandCarusel) { if (id != brandCarusel.Id) { return(NotFound()); } if (brandCarusel.Upload == null) { ModelState.AddModelError("Upload", "The Photo field is required."); } if (ModelState.IsValid) { try { if (brandCarusel.Upload != null) { if (brandCarusel.Upload.ContentType != "image/jpeg" && brandCarusel.Upload.ContentType != "image/png" && brandCarusel.Upload.ContentType != "image/gif") { ModelState.AddModelError("Upload", "You can only download png, jpg or gif file"); return(View(brandCarusel)); } if (brandCarusel.Upload.Length > 1048576) { ModelState.AddModelError("Upload", "The file size can be a maximum of 1 MB"); return(View(brandCarusel)); } var oldFile = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "uploads", brandCarusel.Photo); _fileManager.Delete(oldFile); var fileName = _fileManager.Upload(brandCarusel.Upload, "wwwroot/uploads"); brandCarusel.Photo = fileName; } _context.Update(brandCarusel); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!BrandCaruselExsist(brandCarusel.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(brandCarusel)); }