title: MVC 3 - Image upload fails
body: <p>I am trying write a program that allows users add a image to a database, but it fails. I would be grateful if someone could help me...
  My Index.cshtml file has the following code...</p>
  
  <pre><code> &lt;td&gt;
     @item.Picture1
     &lt;img alt="@Html.Encode(item.Picture1)" src='@(Url.Action(("Picture1")) + 
      ToString())' width="64" height="64" /&gt;
  &lt;/td&gt;
  </code></pre>
  
  <p>Create.cshtml file looks like this...</p>
  
  <pre><code>&lt;div class="editor-field"&gt;
        @Html.EditorFor(model =&gt; model.Picture1)
        &lt;input type="file" id="fileUpload" name="fileUpload" size="23"/&gt;
        @Html.ValidationMessage("Picture1", "*")
  &lt;/div&gt;
  &lt;p&gt;
       &lt;input type="submit" value="Create" /&gt;
  &lt;/p&gt; 
  </code></pre>
  
  <p>In the controller, for Create, I have the following code...</p>
  
  <pre><code>[HttpPost]
      public ActionResult Create([Bind(Exclude = "SubProductCategoryID")] SubProductCategory2 Createsubcat2, FormCollection values)
      {
  
  
          if (ModelState.IsValid)
          {
              if (Request.Files.Count &gt; 0)
              {
               Createsubcat2.Picture1 = (new FileHandler()).uploadedFileToByteArray((HttpPostedFileBase)Request.Files[0]);
              }
              db.AddToSubProductCategory2(Createsubcat2);
              db.SaveChanges();
              return RedirectToAction("/");
          }
          PopulateProductCategoryDropDownList(Createsubcat2.ProductCategoryID);
          return View(Createsubcat2);
      }
  
      public FileResult Image(int id)
      {
          const string alternativePicturePath = @"/Content/question_mark.jpg";
          SubProductCategory2 product = db.SubProductCategory2.Where(k =&gt; k.SubProductCategoryID == id).FirstOrDefault();
  
          MemoryStream stream;
  
          if (product != null &amp;&amp; product.Picture1 != null)
          {
              stream = new MemoryStream(product.Picture1);
          }
          else // If product cannot be found or product doesn't have a picture
          {
              stream = new MemoryStream();
  
              var path = Server.MapPath(alternativePicturePath);
              var image = new System.Drawing.Bitmap(path);
  
              image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
              stream.Seek(0, SeekOrigin.Begin);
          }
  
          return new FileStreamResult(stream, "image/jpeg");
      }
  </code></pre>
  
  <p>FileHandler.cs</p>
  
  <pre><code>public class FileHandler
  {
      /// &lt;summary&gt;
      /// Converts a HttpPostedFileBase into a byte array
      /// &lt;/summary&gt;
      /// &lt;param name="file"&gt;&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public byte[] uploadedFileToByteArray(HttpPostedFileBase file)
      {
          int nFileLen = file.ContentLength;
          byte[] result = new byte[nFileLen];
  
          file.InputStream.Read(result, 0, nFileLen);
  
          return result;
      }    
  </code></pre>
  
  <p>Thanks in advance. I am using <a href="http://blog-dotnet.com/category/ASPNET-MVC.aspx" rel="nofollow">http://blog-dotnet.com/category/ASPNET-MVC.aspx</a> website for reference. I am using VS 2010, ASP.NET 4.0, MVC 3 in C#. SQL Server 2008R2. SubProductCategory2 table has file Picture1, image data type.</p>
  
  <blockquote>
    <p><strong>Edit:</strong>
    Actually working on more I have public byte[] Picture1 { get; set; } in one of the classes. The output I am now getting is System.Byte[] System.Byte[]. How do I fix this? </p>
  </blockquote>