2

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.

Alex
  • 2,927
  • 8
  • 37
  • 56
  • Do you have any JS code that runs before the form is submitted? – Serberuss Jul 12 '13 at 12:29
  • I have another form with drag'n drop upload (and JS script behind) but the action in the controller is different and I don't use this form in this upload. May be it interfere ? – Alex Jul 12 '13 at 12:33
  • Does the object that the enumerable has contain any data or is it just empty? – Serberuss Jul 12 '13 at 12:36
  • I cleaned all my code, I have only what I wrote in my question. `files.Count` is still 1 when I upload nothing. But there's nothing inside... `files.First()` is null. – Alex Jul 12 '13 at 12:56
  • same happening here. did you solve this ? – Bart Calixto Apr 07 '14 at 21:32
  • I think it's a by-design issue. Request.Files got the same behavior. And also, raw http-post.. I checked the MVC source code as well.. You may see this link http://michaelsync.net/2014/04/29/asp-net-mvc-multiple-files-upload-bug-or-by-design-issue – Michael Sync Apr 29 '14 at 09:59
  • 2
    Possible duplicate of [File input empty but an empty "file" shows up in Request.Files](http://stackoverflow.com/questions/34520301/file-input-empty-but-an-empty-file-shows-up-in-request-files) – Haukman Dec 29 '15 at 23:26

1 Answers1

2

Though i am a little late for the party but still. I had a same issue. Found an article on asp.net they said that its by design. http://aspnetwebstack.codeplex.com/workitem/188

This is by design because the request contains that segment which has filename="". If you don't want to have the file created, please remove that segment from the request. I fixed it via the following way.

 if (RelatedFiles.Any())
            {
                foreach (var file in RelatedFiles)
                {
                    if (file != null) // here is just check for a null value.
                    {


                        byte[] uploadedFile = new byte[file.InputStream.Length];
                        file.InputStream.Read(uploadedFile, 0, file.ContentLength);
                        FileInfo fi = new FileInfo(file.FileName);

                        var upload = new UploadedFile
                        {
                            ContentType = file.ContentType,
                            Content = uploadedFile,
                            FileName = fi.Name,
                            ContentExtension = fi.Extension,
                        };

                        newIssuePaper.RelatedDocuments.Add(upload);
                    }
                }
Waqas ali
  • 258
  • 3
  • 7