title: Saving images to database with asp.net mvc 4 + Entity Framework
body: <p>I have this: </p>
  
  <p>Model: </p>
  
  <pre><code> public string Picture { get; set; }
  
   [Column(TypeName = "image")]
   public byte[] Image { get; set; }
  
   [Display(Name = "Display profile Image")]
   public bool DisplayItem { get; set; }
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>&lt;div class="editor-label"&gt;
      @Html.LabelFor(model =&gt; model.DisplayItem)
  &lt;/div&gt;
  &lt;div class="editor-field"&gt;
      @Html.EditorFor(model =&gt; model.DisplayItem)
      @Html.ValidationMessageFor(model =&gt; model.DisplayItem)
  &lt;/div&gt;
  &lt;div class="editor-label"&gt;
      @Html.LabelFor(m =&gt; m.Image)
  &lt;/div&gt;
  
  &lt;input type="file" name="file"/&gt;
  </code></pre>
  
  <p>And controller:</p>
  
  <pre><code>public ActionResult Edit(string UserId)
  {
          string username = User.Identity.Name;
          // Fetch the userprofile
          UserProfile user = db.userProfiles.FirstOrDefault(u =&gt; u.UserName.Equals(username));
          // Construct the viewmodel
  
          return View(user);
  }
  
  [HttpPost]
  public ActionResult Edit(UserProfile userprofile, HttpPostedFileBase file)
  {
          if (file != null &amp;&amp; file.ContentLength &gt; 0)
          {
              // extract only the fielname
              var fileName = Path.GetFileName(file.FileName);
              // store the file inside ~/App_Data/uploads folder
              var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
              file.SaveAs(path);
          }
  
          if (ModelState.IsValid)
          {
              string username = User.Identity.Name;
              // Get the userprofile
              UserProfile user = db.userProfiles.FirstOrDefault(u =&gt; u.UserName.Equals(username));
  
              // Update fields
  
              user.FirstName = userprofile.FirstName;
              user.LastName = userprofile.LastName;
              user.Email = userprofile.Email;
              user.Motto = userprofile.Motto;
  
              user.PlaceOfBirth = userprofile.PlaceOfBirth;
              user.HowManyBikes = userprofile.HowManyBikes;
              user.BesideYourBeth = userprofile.BesideYourBeth;
              user.NicestRide = userprofile.NicestRide;
              user.WorstRide = userprofile.WorstRide;
              user.AmountKmPerYear = userprofile.AmountKmPerYear;
              user.AverageSpeed = userprofile.AverageSpeed;
              user.AbleToChatWhileRiding = userprofile.AbleToChatWhileRiding;
              user.PhoneNumber = userprofile.PhoneNumber;
  
              db.Entry(user).State = EntityState.Modified;
  
              db.SaveChanges();
  
              return RedirectToAction("Edit", "Account");
          }
  
          return View(userprofile);
  }
  </code></pre>
  
  <p>But I want to save the images to the database, and not to only folder. But how to do that in the controller action? THe images are now stored in maps and not in the database</p>
  
  <p>Thank you</p>
  
  <p>this is the Edit:</p>
  
  <pre><code>[HttpPost]
          public ActionResult Edit(UserProfile userprofile, HttpPostedFileBase file)
          {
  
              if (file != null &amp;&amp; file.ContentLength &gt; 0)
              {
  
  
                  // extract only the fielname
                  var fileName = Path.GetFileName(file.FileName);
                  // store the file inside ~/App_Data/uploads folder
                  var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                  file.SaveAs(path);
              }
  
  
  
              if (ModelState.IsValid)
              {
                  string username = User.Identity.Name;
                  // Get the userprofile
                  UserProfile user = db.userProfiles.FirstOrDefault(u =&gt; u.UserName.Equals(username));
  
                  // Update fields
  
                  user.FirstName = userprofile.FirstName;
                  user.LastName = userprofile.LastName;
                  user.Email = userprofile.Email;
                  user.Motto = userprofile.Motto;
  
                  user.PlaceOfBirth = userprofile.PlaceOfBirth;
                  user.HowManyBikes = userprofile.HowManyBikes;
                  user.BesideYourBeth = userprofile.BesideYourBeth;
                  user.NicestRide = userprofile.NicestRide;
                  user.WorstRide = userprofile.WorstRide;
                  user.AmountKmPerYear = userprofile.AmountKmPerYear;
                  user.AverageSpeed = userprofile.AverageSpeed;
                  user.AbleToChatWhileRiding = userprofile.AbleToChatWhileRiding;
                  user.PhoneNumber = userprofile.PhoneNumber;
  
                  db.Entry(user).State = EntityState.Modified;
  
                  db.SaveChanges();
  
                  return RedirectToAction("Edit", "Account");
              }
  
              return View(userprofile);
          }
  </code></pre>
  
  <p>oke, I have it now like this:</p>
  
  <pre><code>[HttpPost]
          public ActionResult Edit(UserProfile userprofile, HttpPostedFileBase file)
          {
  
              if (file != null &amp;&amp; file.ContentLength &gt; 0)
              {               
                  // extract only the fielname
                  var fileName = Path.GetFileName(file.FileName);
                  // store the file inside ~/App_Data/uploads folder
  
                  userprofile.Image = new byte[file.ContentLength];
                  file.InputStream.Read(userprofile.Image, 0, file.ContentLength);
  
                  var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                  file.SaveAs(path);
              }
          etc...
  </code></pre>
  
  <p>I see the images in the folder: ~/App_Data/uploads but not in the database, column: Image -  NULL</p>
  
  <p>I have it now like this:</p>
  
  <pre><code> [HttpPost]
          public ActionResult Edit(UserProfile userprofile, HttpPostedFileBase file)
          {
  
              if (file != null &amp;&amp; file.ContentLength &gt; 0)
              {               
                  // extract only the fielname
                  var fileName = Path.GetFileName(file.FileName);
                  // store the file inside ~/App_Data/uploads folder
  
  
  
                  var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                  file.SaveAs(path);
              }
  
  
  
              if (ModelState.IsValid)
              {
                  string username = User.Identity.Name;
                  // Get the userprofile
                  UserProfile user = db.userProfiles.FirstOrDefault(u =&gt; u.UserName.Equals(username));
  
                  // Update fields
  
                  user.FirstName = userprofile.FirstName;
                  user.LastName = userprofile.LastName;
                  user.Email = userprofile.Email;
                  user.Motto = userprofile.Motto;
  
                  user.PlaceOfBirth = userprofile.PlaceOfBirth;
                  user.HowManyBikes = userprofile.HowManyBikes;
                  user.BesideYourBeth = userprofile.BesideYourBeth;
                  user.NicestRide = userprofile.NicestRide;
                  user.WorstRide = userprofile.WorstRide;
                  user.AmountKmPerYear = userprofile.AmountKmPerYear;
                  user.AverageSpeed = userprofile.AverageSpeed;
                  user.AbleToChatWhileRiding = userprofile.AbleToChatWhileRiding;
                  user.PhoneNumber = userprofile.PhoneNumber;
  
                  userprofile.Image = new byte[file.ContentLength];
                  file.InputStream.Read(userprofile.Image, 0, file.ContentLength);
  
                  db.Entry(user).State = EntityState.Modified;               
  
                  db.SaveChanges();
  
                  return RedirectToAction("Edit", "Account");
              }
  
              return View(userprofile);
          }
  </code></pre>
  
  <p>but still in database Image is NULL</p>