File input empty but an empty “file” shows up in Request.Files

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x