3

I have a view that has the following file input:

<input type="file" name="SchemaFiles" id="SchemaFiles" class="form-control" multiple/>

My model has this attribute:

  public List<HttpPostedFileBase> SchemaFiles { get; set; }

When I select one or two files for this input, everything works fine. However, if I don't select any files and submit the form, then SchemaFiles.Count is 1, and SchemaFiles[0] is null (but does exist). ModelState.IsValid is true.

Is there a way to avoid this?

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Dave
  • 1,645
  • 2
  • 23
  • 39

1 Answers1

2

This is by-design and looks like the root cause is the browser. See these links for reference:

Community
  • 1
  • 1
Haukman
  • 3,726
  • 2
  • 21
  • 33
  • I'll see if the browsers have a bug report or explanation. – Dave Dec 29 '15 at 23:06
  • I think most browsers will do it this way, probably legacy reasons that will just stick. My point is that the data coming in over HTTP has an empty filename when no files are selected (at least according to these posts) and the correct way to solve this is to filter it out in your server code. In general you should always distrust data coming from the browser, always check and filter out invalid data. – Haukman Dec 29 '15 at 23:24