title: Multiple Image Files Upload In ASP.NET MVC
body: <p>I am trying to upload multiple images and then store them in database.</p>
  
  <p>The problem is all the images are being saved in the images folder. But in the database, the name of the last image uploaded is being shown.</p>
  
  <p>For example if i uploaded 3 images 1 2 and 3.
  all the three images will be saved in the folder but the name of only last image is uploaded is shown in database.</p>
  
  <p>Following is the code of <strong>Controller:</strong></p>
  
  <pre><code>public ActionResult Create(IEnumerable&lt;HttpPostedFileBase&gt; files)
  {
      foreach (var filed in files)        
      {
          if (filed != null &amp;&amp; filed.ContentLength &gt; 0)
          {
              var gphoto = new PkgPhotoGallery
              {
                  FileName = Guid.NewGuid().ToString() + Path.GetExtension(filed.FileName),
                  FileType = FileType.Image
              };
              filed.SaveAs(Path.Combine(Server.MapPath("~/PackagePhotoGallery"), gphoto.FileName));
              packages.PkgPhotoGalleries = new List&lt;PkgPhotoGallery&gt;();   
              packages.PkgPhotoGalleries.Add(gphoto);
          }
      }
      db.Packages.Add(packages);
      db.SaveChanges();
      return RedirectToAction("Index");
  }
  </code></pre>
  
  <p>Follwoing is the code of <strong>Model</strong></p>
  
  <pre><code>public class PkgPhotoGallery
  {
      [Key]
      public int FileId { get; set; }
      public  string FileName { get; set; }
      public  FileType FileType { get; set; }
      public int? PackageId { get; set; }
      public virtual Packages Packages { get; set; }
  }
  </code></pre>
  
  <p>Code of <strong>View</strong></p>
  
  <pre><code>&lt;input type="file" name="files"  multiple/&gt;
  </code></pre>