示例#1
0
        public async Task <IActionResult> ChangeProfileImage()
        {
            try
            {
                var id   = HttpContext.User.FindFirst(ClaimTypes.Email).Value;
                var user = await _userManager.FindByEmailAsync(id);

                var files = Request.Form.Files;

                var filecount = files.Count;

                if (filecount > 0)
                {
                    using (var memoryStream = new MemoryStream())
                    {
                        var file = files[0];
                        await file.CopyToAsync(memoryStream);

                        var image = memoryStream.ToArray();

                        user.AvatarImage = image;

                        await _userManager.UpdateAsync(user);
                    }
                    var updatedProfile = _userCommand.GetUserProfile(user).Result;
                    return(Ok(updatedProfile));
                }

                return(StatusCode((int)HttpStatusCode.InternalServerError, "No Image Found"));
            }
            catch (Exception e)
            {
                _logger.LogError($"error while changing profile for user: {e}");
                return(StatusCode((int)HttpStatusCode.InternalServerError, "error while changing profile for user"));
            }
        }