public ActionResult Avatar() { SetBreadCrumb("Avatar"); var model = new AvatarViewModel() { HeightMax = SiteConfig.AvatarHeight.ToInt(), WidthMax = SiteConfig.AvatarWidth.ToInt() }; switch (CurrentUser.UserProfile.AvatarType) { case "Upload": model.AvatarType = AvatarType.Upload; break; case "Url": model.AvatarType = AvatarType.Url; model.Url = CurrentUser.UserProfile.Avatar; break; case "None": default: model.AvatarType = AvatarType.None; break; } return View(model); }
public ActionResult Avatar(AvatarViewModel model, string ExternalImageURL) { switch (model.AvatarType) { case AvatarType.Url: if (string.IsNullOrWhiteSpace(model.Url)) ModelState.AddModelError("Url", "Enter an avatar url."); break; case AvatarType.Upload: List<string> validFormats = new List<string> { "image/png", "image/x-png", "image/gif", "image/x-gif", "image/jpeg", "image/x-jpeg", "image/jpg", "image/x-jpg", }; if (model.Image == null || model.Image.ContentLength == 0) ModelState.AddModelErrorFor<AvatarViewModel>(m => m.Image.FileName, "Please select an image to upload."); else if (!validFormats.Contains(model.Image.ContentType)) ModelState.AddModelErrorFor<AvatarViewModel>(m => m.Image, "Invalid file format; only gif,png,jpeg,jpg are allowed."); break; case AvatarType.None: default: break; } if (IsModelValidAndPersistErrors()) { switch (model.AvatarType) { case AvatarType.Upload: string uploadedFileName = _fileServices.UploadAvatar(model.Image); _userServices.UpdateAvatarToUpload(CurrentUser.UserID, uploadedFileName); break; case AvatarType.Url: _userServices.UpdateAvatarToUrl(CurrentUser.UserID, model.Url); break; case AvatarType.None: default: _userServices.UpdateAvatarToNone(CurrentUser.UserID); break; } SetSuccess("Profile Settings Updated"); } return RedirectToSelf(); }