public ActionResult UploadImage(UploadImageViewModel model) { //Check if all simple data annotations are valid if (ModelState.IsValid) { //Prepare the needed variables Bitmap original = null; var name = "newimagefile"; var errorField = string.Empty; if (model.IsUrl) { errorField = "Url"; name = GetUrlFileName(model.Url); original = GetImageFromUrl(model.Url); } else if (model.File != null) { errorField = "File"; name = Path.GetFileNameWithoutExtension(model.File.FileName); original = Bitmap.FromStream(model.File.InputStream) as Bitmap; } //If we had success so far if (original != null) { var img = CreateImage(original, model.X, model.Y, model.Width, model.Height); var fileName = Guid.NewGuid().ToString(); var oldFilepath = userService.GetUser(User.Identity.GetUserId()).ProfilePicUrl; var oldFile = Server.MapPath(oldFilepath); //Demo purposes only - save image in the file system var fn = Server.MapPath("~/Content/ProfilePics/" + fileName + ".png"); img.Save(fn, System.Drawing.Imaging.ImageFormat.Png); userService.SaveImageURL(User.Identity.GetUserId(), "~/Content/ProfilePics/" + fileName + ".png"); if (System.IO.File.Exists(oldFile)) { System.IO.File.Delete(oldFile); } return RedirectToAction("UserProfile", new { id = User.Identity.GetUserId() }); } else //Otherwise we add an error and return to the (previous) view with the model data ModelState.AddModelError(errorField, Resources.UploadError); } return View("ImageUpload", model); }
public void Upload_Image_Post() { var userManager = new UserManager<ApplicationUser>(new TestUserStore()); UploadImageViewModel image = new UploadImageViewModel() { IsFile = true, UserId = "402bd590-fdc7-49ad-9728-40efbfe512ec", LocalPath = "dddd" }; AccountController controller = new AccountController(userService, userProfileService, goalService, updateService, commentService, followRequestService, followUserService, securityTokenService,userManager); ViewResult result = controller.UploadImage(image) as ViewResult; Assert.IsNotNull(result); Assert.IsInstanceOfType(typeof(UploadImageViewModel), result.ViewData.Model, "WrongType"); Assert.AreEqual("ImageUpload", result.ViewName); }
public ActionResult ImageUpload() { UploadImageViewModel imageVM = new UploadImageViewModel(); imageVM.LocalPath = userService.GetUser(User.Identity.GetUserId()).ProfilePicUrl; return PartialView(imageVM); }