user3913587
user3913587

Reputation: 41

how to check for an empty .csv extension file in C#

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

Answers (3)

var file = controller.Request.Files[0];
if(file.ContentLength <= 2) 
{ // do something 

 }

Upvotes: -1

smohamed
smohamed

Reputation: 3314

I think this would work:

if( new FileInfo( "file" ).Length == 0 )
{
//your code
}

Upvotes: 0

usr
usr

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

Related Questions