title: C#, ASP.NET MVC: Multiple image upload and uploaded images all remain the same problem
body: <p>I'm trying to upload multiple images, I managed to upload as many images as I wanted. My only problem is that all the images I chose duplicate by copying the first one. I could not find the error in my codes.</p>
  <p>My <code>UploadImage</code> method:</p>
  <pre><code>public static string UploadImage(string serverPath, IEnumerable&lt;HttpPostedFileBase&gt; files)
  {
          Guid uniqueName = Guid.NewGuid(); 
          serverPath = serverPath.Replace(&quot;~&quot;, string.Empty);
          string filePath;
  
          foreach (HttpPostedFileBase item in files)
          {
              if (files != null &amp;&amp; item.ContentLength &gt; 0)
              {
                  string extension = Path.GetExtension(item.FileName);
                  string fileName = $&quot;{uniqueName}{extension}&quot;;
  
                  if (extension.ToLower() == &quot;.jpeg&quot; || extension.ToLower() == &quot;.gif&quot; || extension.ToLower() == &quot;.png&quot; || extension.ToLower() == &quot;.jpg&quot;)
                  {
                      if (File.Exists(HttpContext.Current.Server.MapPath(serverPath + fileName))) 
                      {
                          return &quot;Already exists from same file&quot;;
                      }
                      else
                      {
                          filePath = Path.Combine(HttpContext.Current.Server.MapPath(serverPath),fileName);
                          item.SaveAs(filePath);
                          return serverPath+fileName;
                      }
                  }
                  else
                  {
                      return &quot;Not the selected picture.&quot;;
                  }
              }
              else
              {
                  return &quot;No File Selected&quot;;
              }    
      }
  
      return &quot;&quot;;
  }
  </code></pre>
  <p>This is the <code>ImageController</code>:</p>
  <pre><code>[HttpPost]
  public ActionResult Index(TestClass model, IEnumerable&lt;HttpPostedFileBase&gt; files)
  {
          foreach (HttpPostedFileBase item in files)
          {
              model.ImagePath = ImageUploader.UploadImage(&quot;~/Images/&quot;, files) ;
              db.TestClass.Add(model);
              db.SaveChanges();
          }
  
          return View();
  }
  </code></pre>
  <p>and the <code>ImageClass</code>:</p>
  <pre><code>public class TestClass
  {
      public int ID { get; set; }
  
      public string Name { get; set; }
      public string ImagePath { get; set; }
  }
  </code></pre>
  <p>and the view <code>Index.cshtml</code>:</p>
  <pre><code>@using (Html.BeginForm(&quot;Index&quot;,&quot;Image&quot;,FormMethod.Post, new { enctype=&quot;multipart/form-data&quot;}))
  {
  &lt;div&gt;
       Name
  &lt;/div&gt;
  &lt;div&gt;
      @Html.TextBoxFor(x=&gt;x.Name)
  &lt;/div&gt;
  &lt;div&gt;
      &lt;input multiple type=&quot;file&quot; name=&quot;files&quot; value=&quot;Browse&quot; /&gt;
  &lt;/div&gt;
  &lt;div&gt;
      &lt;button class=&quot;btn btn-primary&quot;&gt;Save&lt;/button&gt;
  &lt;/div&gt;
  }
  </code></pre>