title: unable to upload multiple db images with asp.net mvc
body: <p>I am trying to upload several db images onto the SQL Server 2008R2. I am using ASP.NET MVC 3 in C#. What is happening is that I getting the images displayed but the problem is that the second image is being displayed as twice. So it is duplicate. I am not sure why the first image is not being displayed.</p>
  
  <p>My SubProductCategory4 Table has the following columns (for simplicity sake)...</p>
  
  <p>Column Names: Image1 and Image2 has DataTypes varbinary(MAX), another column Name: ImageMimeType has DataTypes varchar(50).</p>
  
  <p>My Controller has the following code for Create method...</p>
  
  <pre><code>[HttpPost]
      public ActionResult Create([Bind(Exclude = "SubProductCategoryFourID")] SubProductCategory4 Createsubcat4, IEnumerable&lt;HttpPostedFileBase&gt; files, FormCollection collection)
      {
          if (ModelState.IsValid)
          {
             foreach (string inputTagName in Request.Files)
             {
  
       if (Request.Files.Count &gt; 0) // tried Files.Count &gt; 1 did 
                                            // not solve the problem
                      {
                          Createsubcat4.Image1 = (new FileHandler()).uploadedFileToByteArray((HttpPostedFileBase)Request.Files[inputTagName]);
                          Createsubcat4.Image2 = (new FileHandler()).uploadedFileToByteArray((HttpPostedFileBase)Request.Files[inputTagName]);
                          // var fileName = Path.GetFileName(inputTagName);
                          //var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                      }
                      // moved db.AddToSubProductCategory4(Createsubcat4);
                      // here  but did not solve the problem
             }
              db.AddToSubProductCategory4(Createsubcat4);
              db.SaveChanges();
              return RedirectToAction("/");
          }
  
  
     //someother code
  
          return View(Createsubcat4);
      } 
  </code></pre>
  
  <p>GetImage method...</p>
  
  <pre><code>public FileResult GetImage(int id)
      {
          const string alternativePicturePath = @"/Content/question_mark.jpg";
          MemoryStream stream;
          MemoryStream streaml;
  
          SubProductCategory4 z = db.SubProductCategory4.Where(k =&gt; k.SubProductCategoryFourID == id).FirstOrDefault();
  
          if ((z != null &amp;&amp; z.Image1 != null) &amp;&amp; (z != null &amp;&amp; z.Image2 != null))
          {
  
                  stream = new MemoryStream(z.Image1);
                  streaml = new MemoryStream(z.Image2);
          }
  
          else
          {
                var path = Server.MapPath(alternativePicturePath);
  
               foreach (byte item in Request.Files)
                { 
                  HttpPostedFileBase file = Request.Files[item];
                  if (file.ContentLength == 0)
                  {
                      continue;
                  }
               }
  
              stream = new MemoryStream();
              var imagex = new System.Drawing.Bitmap(path);
              imagex.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
              stream.Seek(0, SeekOrigin.Begin);
  
             /* streaml = new MemoryStream();
              var imagey = new System.Drawing.Bitmap(path);
              imagey.Save(streaml, System.Drawing.Imaging.ImageFormat.Jpeg);
              streaml.Seek(0, SeekOrigin.Begin);*/
          }
  
         return new FileStreamResult(stream,"image/jpg");
  
      }
  </code></pre>
  
  <p>FileHandler.cs</p>
  
  <pre><code>public class FileHandler
  {
      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>create.cshtml...</p>
  
  <pre><code>     @using (Html.BeginForm("Create", "ProductCategoryL4", "GetImage",  
       FormMethod.Post, new { enctype = "multipart/form-data" }))    
        //some code then...
       &lt;div class="editor-field"&gt;
       @Html.EditorFor(model =&gt; model.Image1)
      &lt;input type="file" id="fileUpload1" name="fileUpload1" size="23"/&gt;
       @Html.ValidationMessageFor(model =&gt; model.Image1)
      &lt;/div&gt;
  
       &lt;div class="editor-field"&gt;
       @Html.EditorFor(model =&gt; model.Image2)
       &lt;input type="file" id="fileUpload2" name="fileUpload2" size="23"/&gt;
       @Html.ValidationMessageFor(model =&gt; model.Image2)
      &lt;/div&gt;
  </code></pre>
  
  <p>index.cshtml...</p>
  
  <pre><code>&lt;img src="@Url.Action("GetImage", "ProductCategoryL4", new { id =   
  item.SubProductCategoryFourID })" alt="" height="100" width="100" /&gt; 
  &lt;/td&gt;
    &lt;td&gt;
      &lt;img src="@Url.Action("GetImage", "ProductCategoryL4", new { id = 
      item.SubProductCategoryFourID })" alt="" height="100" width="100" /&gt; 
    &lt;/td&gt;
  </code></pre>
  
  <p>I am using using VS2010, ASP.NET MVC3 in C# with SQL Server 2008R2. Thanks in advance but please only respond if you know the answer. If there is a better way of doing this please let me know.  </p>