Why IEnumerable<HttpPostedFileBase> count is 1 when I upload 0 files?

Topics: ASP.NET MVC
Apr 29, 2014 at 4:08 AM
Edited Apr 29, 2014 at 10:37 AM
Is it the known issue?

http://stackoverflow.com/questions/17614892/why-ienumerablehttppostedfilebase-count-is-1-when-i-upload-0-files

I have a multiple upload form and I want to check if there is any files when I launch the upload. Here is my code.

View :
@using (Html.BeginForm("Upload", "Home", FormMethod.Post, 
                       new { enctype = "multipart/form-data"}))
{
    <input name="files" type="file" multiple="multiple" />
    <input type="submit" value="Upload" />
}
Controller :
[HttpPost]
public ActionResult Upload(IEnumerable<HttpPostedFileBase> files)
{
    if (files.Count() > 0) Console.WriteLine(files.Count()); // display 1
    if(files.Any()) Console.WriteLine(files.Any()); // display true
    if (files.First() == null) Console.WriteLine("first null"); // display "first null"

    return View();
}
Why my program display results like that when I submit an empty form ? I'll probably check with JS my field, but I want to understand what is these data in my IEnumerable<HttpPostedFileBase>. Thank you.

I did a few research on this and posted them on my blog http://michaelsync.net/2014/04/29/asp-net-mvc-multiple-files-upload-bug-or-by-design-issue . If it's not the by-design issue then I like to contribute the patch for this. I contributed it once for EF so you guys already have my signed paper.
May 8, 2014 at 8:05 AM
Any update? guys
Developer
May 8, 2014 at 7:34 PM
Hi,

This is by design. This is part of System.Web and so we are not too inclined to fix this issue currently. A similar related issue was filed against Web API. You can check the resolution information on it.

http://aspnetwebstack.codeplex.com/workitem/188

Thanks,
Kiran
May 9, 2014 at 2:01 AM
Great! Thanks!