10

I have a form on an HTML page that a user needs to use to upload a file which posts to an ASPX page. In the code behind, I want to test if a file has actually been loaded.

if (Request.Files.Count > 0)
{
    DoStuff(Request.Files[0]);
}
else
{
    throw new Exception("A CSV file must be selected for upload.");
}

I am never getting to the else. Is this just how ASP.NET operates? If I have a input element of type file, is it always going to upload a "file" even if one is not selected?

What's the proper way to do this? Maybe this?

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.");
}
kzh
  • 19,810
  • 13
  • 73
  • 97

5 Answers5

9

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.");
}
kzh
  • 19,810
  • 13
  • 73
  • 97
  • Unfortunately, if you access the Files property at all, you can't subsequently call Request.GetBufferlessInputStream: "This method or property is not supported after HttpRequest.Form, Files, InputStream, or BinaryRead has been invoked" – dudeNumber4 Oct 03 '14 at 19:44
  • @dudeNumber4 So is selected answer incorrect? I don't have Visual Studio on this computer right now to investigate. – kzh Oct 03 '14 at 20:00
  • Well, it's incorrect for me because I need to access that method in order to chunk the (large) uploaded files so they won't overload server memory. If you don't need to access that method, I suppose you're fine. – dudeNumber4 Oct 06 '14 at 14:54
7

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.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
2

You should use the FileUpload control and check .HasFiles to see if anything was uploaded.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.aspx

Matt S
  • 395
  • 1
  • 2
  • 11
  • I wish I could use this, but my aspx page is being called from a static html page. :( – kzh Dec 10 '10 at 18:15
1

Everything was in place as mentioned above. Adding FormMethod.Post solved my issue.

FormMethod.Post, new { enctype = "multipart/form-data"}
Koby Douek
  • 16,156
  • 19
  • 74
  • 103
Rizwan
  • 11
  • 1
-1

I would also make sure that the data being return by the .count method is not a string. A string value of '0' is always greater than a int value of 0; which would always return true in that condition.

I would try typecasting the .count return as an int to make sure that proper types are being compared. Even a string '-1' has a higher bit value than int zero.

just a thought, though I could be wrong....

cntlscrut
  • 1
  • 1