HTTPRequest.Files.Count Never Equals Zero
Maybe just this will do:
if (Request.Files.Count > 0 && Request.Files[0].ContentLength > 0)
{
DoStuff(Request.Files[0]);
}
else
{
throw new Exception("A CSV file must be selected for upload.");
}
Request.Files.Count
always contains the no. of <input type="file">
elements in your form, wrapped in a Key:Value
store.
Hence, if your posted form does not contain any <input type="file">
tags, then Request.Files.Count
will return 0
.
Each Key
is the name
of the <input type="file" name="OneOfTheKeys">
and the value is of type HttpPostedFileWrapper
.
See here to learn about HttpPostedFileWrapper
.