public static ProfilePics GetUserProfilePic(string username) { ProfilePics pp = new ProfilePics(); int id = FetchUserId(username); using (UsersContext db = new UsersContext()) { ProfilePics profilepic = db.ProfilePics.FirstOrDefault(u => u.userid == id && u.status == 1); // Check if user already exists if (profilepic == null) { string path = HostingEnvironment.MapPath(@"~/"); Debug.WriteLine(path); string filename = path + "Images\\defaultimage.png"; Debug.WriteLine(filename); using (Image image = Image.FromFile(filename)) { profilepic = new ProfilePics(); profilepic.userid = id; using (MemoryStream m = new MemoryStream()) { image.Save(m, image.RawFormat); byte[] imageBytes = m.ToArray(); // Convert byte[] to Base64 String // base64String = Convert.ToBase64String(imageBytes); profilepic.pic = imageBytes; } } } return(profilepic); } }
public ActionResult Upload(UserDetailsModel IG) { if (!Request.IsAuthenticated) { return(RedirectToAction("Login", "Account")); } // Apply Validation Here //Set the other profile pictures as inactive //Save the profile image if (IG.File.ContentLength > (2 * 1024 * 1024)) { ModelState.AddModelError("CustomError", "File size must be less than 2 MB"); return(RedirectToAction("Profile")); } /* if (!(IG.File.ContentType == "image/jpeg" || IG.File.ContentType == "image/gif")) * { * ModelState.AddModelError("CustomError", "File type allowed : jpeg and gif"); * return RedirectToAction("Profile"); * }*/ Helpers.UpdateProfilePicStatus(System.Web.HttpContext.Current.User.Identity.Name); byte[] data = new byte[IG.File.ContentLength]; IG.File.InputStream.Read(data, 0, IG.File.ContentLength); ProfilePics details = new ProfilePics(); int id = Helpers.FetchUserId(System.Web.HttpContext.Current.User.Identity.Name); details.userid = id; details.pic = data; details.datecreated = DateTime.Now; details.datemodified = DateTime.Now; details.status = 1;//Status 1 means the profile picture is the active 1. using (UsersContext dc = new UsersContext()) { dc.ProfilePics.Add(details); dc.SaveChanges(); } return(RedirectToAction("Profile")); }