Reputation: 41
I have an input type file in my .aspx page.
<input type="file" id="BatchImportFile" runat="server">
When I am trying to check the File.PostedFile.ContentLength
property for an empty file it is always showing value as 2 even if I upload an empty file.
I want to check an empty .csv
file .
Upvotes: 3
Views: 2012
Reputation: 1
var file = controller.Request.Files[0];
if(file.ContentLength <= 2)
{ // do something
}
Upvotes: -1
Reputation: 3314
I think this would work:
if( new FileInfo( "file" ).Length == 0 )
{
//your code
}
Upvotes: 0
Reputation: 171178
The file that you uploaded simply was not empty. 2 bytes is the length of a Windows line terminator. Maybe it contains an empty line. Unicode BOM headers are also 2 bytes in length.
Use String.IsNullOrWhiteSpace
to check for this case.
Upvotes: 5