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