public IActionResult OnPost([FromForm] UploadImageDto imageDto) { if (_userManager.Users.FirstOrDefault(u => u.UserName == imageDto.UserName) == null) { return(Unauthorized()); } foreach (var img in imageDto.ImageFiles) { if (!_fileRepository.SaveImage(imageDto.UserName, img)) { throw new Exception("Image Upload Failed"); } } return(RedirectToPage("AuthorDashboard")); }
public async Task <IActionResult> SaveImage([FromBody] IFormFile image) { if (image == null) { return(BadRequest()); } var userEntity = await _userManager.GetUserAsync(User); if (userEntity == null) { return(Unauthorized()); } var imageEntity = new ImageData() { UserId = userEntity.Id, UserName = userEntity.UserName, FileName = image.FileName, ContentType = image.ContentType }; if (_fileRepository.SaveImage(userEntity.UserName, image)) { _dbContext.Images.Add(imageEntity); if (await _dbContext.SaveChangesAsync() < 0) { throw new Exception("Failed to save file records"); } } else { throw new Exception("Failed to save file."); } return(CreatedAtRoute("ImageContent", new { userName = userEntity.UserName, fileName = image.FileName }, imageEntity)); }