0

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

Martin Hansen Lennox
  • 2,837
  • 2
  • 23
  • 64

2 Answers2

0

Probably not the answer you were looking for, but I would recommend a different approach. That blog post is pretty old. I use the plupload jquery plugin for doing multi-file uploads. Very easy to use, very easy to implement. Here is an example that got posted in another question for how to implement using MVC. In the example the Upload method will be called for each file you are trying to upload.

Using plupload with MVC3

Hope that helps.

Community
  • 1
  • 1
Ben Tidman
  • 2,129
  • 17
  • 30
  • Thanks Ben, yes it is old.. chose the approach because it would "only take 5 mins" :) I welcome any recommendations and will check out the link tomorrow, – Martin Hansen Lennox Jan 07 '13 at 00:24
-1

This might not answers your question, but it's another way of uploading files.

Try this:

[HttpPost]
public ActionResult Upload(IEnumerable<HttpPostedFileBase> files)
{
    if (files != null && files.Count() > 0)
    {
        foreach (var uploadedFile in files)
        {
            if (uploadedFile.ContentType != "image/vnd.dwg") 
            {
                return RedirectToAction("List");
            }

            var appData = Server.MapPath("~/app_data");
            var filename = Path.Combine(appData, Path.GetFileName(uploadedFile.FileName));
            uploadedFile.SaveAs(filename);                    
        }
    }

    return RedirectToAction("Success");
}

and modify the markup so that the file inputs are named files:

<input type="file" name="files" />
<input type="file" name="files" />
...// many inputs type file

"Orginally post by Darin Dimitrov", link : Upload multiple files using HttpFileCollectionBase issue with C# and MVC3

Community
  • 1
  • 1
Jack
  • 2,600
  • 23
  • 29
  • 1
    The enumerator on the HttpFileCollection returns the keys (names) of the files, not the HttpPostedFile objects. Once you get the key, use the Item ([]) property with the key (filename) to get the HttpPostedFile object. – heads5150 Jan 07 '13 at 00:52