public ActionResult UploadImage(UploadModel upload) { if (ModelState.IsValid) { Bitmap original = null; var name = "newimagefile"; var errorField = string.Empty; string foldername = "default"; if (upload.IsUrl) { errorField = "Url"; name = GetUrlFileName(upload.Url); original = GetImageFromUrl(upload.Url); } else if (upload.IsFlickr) { errorField = "Flickr"; name = GetUrlFileName(upload.Flickr); original = GetImageFromUrl(upload.Flickr); } else if (upload.File != null) // model.IsFile { errorField = "File"; name = Path.GetFileNameWithoutExtension(upload.File.FileName); original = Bitmap.FromStream(upload.File.InputStream) as Bitmap; foldername = Server.MapPath("~/Content/images/Profile/"+upload.userID.ToString()); // Create Directory /userID DirectoryInfo directory = new DirectoryInfo(foldername); if (!Directory.Exists(foldername)) { directory.Create(); } } // User Find And Update using (var db = new DataModelContext()) { tbl_userProfile userprofile = db.tbl_userProfile.Find(upload.userID); if (userprofile != null) { userprofile.image = name; db.Entry(userprofile).State = EntityState.Modified; db.SaveChanges(); } } //If we had success so far if (original != null) { var img = CreateImage(original, upload.X, upload.Y, upload.Width, upload.Height); //Demo purposes only - save image in the file system var fn = Server.MapPath(foldername+"\\profile"+".png"); img.Save(fn, System.Drawing.Imaging.ImageFormat.Png); //Redirect to index return RedirectToAction("Index"); } } return View(upload); }
public ActionResult userProfile(tbl_userProfile userprofile) { if (ModelState.IsValid) { using (var db = new DataModelContext()) { tbl_userProfile userprof = (from profile in db.tbl_userProfile where profile.userID == userprofile.userID select profile).FirstOrDefault(); if (userprof == null)// user not found in database, insert new data { userprofile.createDate = DateTime.Now; db.tbl_userProfile.Add(userprofile); db.SaveChanges(); return RedirectToAction("UsersList"); } else // already has user data Update ! { userprof.nameSurname = userprofile.nameSurname; userprof.phone = userprofile.phone; userprof.adress = userprofile.adress; userprof.createDate = DateTime.Now; db.Entry(userprof).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("UsersList"); } } } else { ModelState.AddModelError("ProfileAdd", "Do not new user profile!.."); } return View(userprofile); }
public ActionResult EditUser(tbl_users user) { if (ModelState.IsValid) { using (var db = new DataModelContext()) { user.typeID = Convert.ToByte(user.typeID); //Utility cryptor = new Utility(); // Encrypt User Password //string encrypt = cryptor.CreatePasswordHash(user.password, user.saltPass = cryptor.GenerateSalt(58), "SHA1"); string encrypt = FormsAuthentication.HashPasswordForStoringInConfigFile(user.password, "md5"); user.password = encrypt; db.Entry(user).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("UsersList"); } } else { return RedirectToAction("EditUser"); } }