I'm trying to upload multiple files and iterate through them in a View. But rather than upload X number of files, it uploads the first file X many times (e.g. 3 times if I've uploaded 3 files). In the destination folder, only a single file is saved.
It seems to recognise the number of files present, so why is it not iterating through them?
I should mention that I'm quite new to asp.net/c# (more used to classic/vb) so apologies if I'm missing something obvious...
Code:
public class ViewDataUploadFilesResult
{
public string Name { get; set; }
public int Length { get; set; }
}
public ActionResult UploadMultipleFiles()
{
var r = new List<ViewDataUploadFilesResult>();
foreach (string file in Request.Files)
{
HttpPostedFileBase hpf = Request.Files[file];//as HttpPostedFileBase;
if (hpf.ContentLength == 0)
continue;
string basepath = Server.MapPath("/Images");
string savedFileName = Path.Combine(basepath, Path.GetFileName(hpf.FileName));
hpf.SaveAs(savedFileName);
r.Add(new ViewDataUploadFilesResult()
{
Name = savedFileName,
Length = hpf.ContentLength
});
}
return View(r);
}
Example Result:
<li>Uploaded: C:\Users...sonatrach.jpg totalling 3581 bytes.</li>
<li>Uploaded: C:\Users...sonatrach.jpg totalling 3581 bytes.</li>
<li>Uploaded: C:\Users...sonatrach.jpg totalling 3581 bytes.</li>
I have been working through Scott Hanselman's post here: http://www.hanselman.com/blog/ABackToBasicsCaseStudyImplementingHTTPFileUploadWithASPNETMVCIncludingTestsAndMocks.aspx