public IActionResult UploadImage(UploadImage uploadedImage, [FromRoute] int id) { if (!ModelState.IsValid) { return(View(uploadedImage)); } var image = uploadedImage.Image; byte[] imageData = null; using (var binaryReader = new BinaryReader(image.OpenReadStream())) { imageData = binaryReader.ReadBytes((int)image.Length); } var imageForDb = ImageHelper.AddOleHeader(imageData); var category = _categoryService.GetCategoryById(id); category.Picture = imageForDb; var result = _categoryService.UpdateCategory(category); if (result == false) { return(RedirectToAction("Error", "Home")); } return(RedirectToAction("Index")); }
public async Task <ActionResult> UpdateImage([FromForm] IFormFile image, [FromRoute] int id) { byte[] imageData = null; if (image.Length == 0) { return(BadRequest()); } if (!await _categoryService.CategoryIsExistsAsync(id)) { return(NotFound()); } using (var binaryReader = new BinaryReader(image.OpenReadStream())) { imageData = binaryReader.ReadBytes((int)image.Length); } var imageForDb = ImageHelper.AddOleHeader(imageData); try { var category = await _categoryService.GetCategoryByIdAsync(id); category.Picture = imageForDb; await _categoryService.UpdateCategoryAsync(category); return(Ok()); } catch (Exception ex) { _logger.LogError(ex.Message); var category = await _categoryService.GetCategoryByIdAsync(id); if (category == null) { return(NotFound()); } } return(BadRequest()); }