예제 #1
0
        public async Task <JsonResult> UploadAvatar(string imageBase64)
        {
            if (string.IsNullOrEmpty(imageBase64))
            {
                return(Json(new JsonResultModel {
                    Message = "图片信息错误!"
                }));
            }
            if (imageBase64.Length > WebSysConfig.AvatarMaxSize)
            {
                return(Json(new JsonResultModel {
                    Message = "图片过大,换个图试试!"
                }));
            }
            try
            {
                string oldFile  = string.Empty;
                string fileName = Upload.UploadAvatar(imageBase64);

                var user = Users.GetUserById(CurrentUserInfo.Id);
                if (user != null && !user.IsNull)
                {
                    oldFile = user.Avatar;
                }

                bool status = UserPublic.UpdateAvatar(CurrentUserInfo.Id, fileName);
                UsersLogin.RefreshCookieUserInfo(CurrentUserInfo.Id);
                if (status)
                {
                    if (!string.IsNullOrEmpty(oldFile))
                    {
                        await Upload.DeleteAvatar(oldFile);
                    }
                    return(Json(new JsonResultModel {
                        ResultState = true, Message = "保存成功!"
                    }));
                }
            }
            catch (Exception ex)
            {
                return(Json(new JsonResultModel {
                    Message = ex.Message
                }));
            }
            return(Json(new JsonResultModel {
                Message = "保存失败!"
            }));
        }