public ActionResult Create(Post post) { if (ModelState.IsValid) { HttpPostedFileBase file = Request.Files["ImageData"]; post.image = file.ConvertToBytes(); var p = new Post { IDPost = post.IDPost, Title = post.Title, Description = post.Description, Date = DateTime.Now, isPublic = false, image = post.image, ApplicationUser = db.Users.First(u => u.UserName == User.Identity.Name), ApplicationUserId = db.Users.First(u => u.UserName == User.Identity.Name).Id }; db.posts.Add(p); db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ApplicationUserId = new SelectList(db.Users, "Id", "Email", post.ApplicationUserId); return(View(post)); }
/// <summary> /// Convert a HttpPostedFileBase to an byte array. To compress the byte array, specify true when calling the extension. /// </summary> /// <param name="file">Posted file to apply this to</param> /// <returns>byte[]</returns> public static byte[] ConvertToBytes(this HttpPostedFileBase file) { if (file == null) { throw new ArgumentNullException(nameof(file), "The passed in file parameter was null."); } return(file.ConvertToBytes(false)); }