I've been researching and going through stackoverflow for a few hours now. There are some solutions out there but they don't work in all situations. Let me start by explaining my problem first.
My problem is I am trying to upload a file and the file could be any of following options
- doc
- docx
- xls
- xlsx
Peoples have mostly suggested using following approach
if (FileUpload.FileContent.Length == 0)
I tried this approach on an empty docx file which is newer format of Microsoft Word. Surprisingly it failed. When I debugged the code i actually saw there was content in the file and when I edited it in notepad it turned out true. Similar is the case with 97-2003 format which is doc and also with newer excel format which is xlsx and old 97-2003 format which is xls.
It seems very obvious checking length of content will not work. I have not worked about pdf but its highly likely similar is the case with pdf as well. It may have its own data.
Now the big question is how do we check if the file has actual content or not.
Note that user can try to upload a file which may have content but that content may be nothing more then white space. Consider space, tab, carriage return or new lines. Essentially a file with white spaces is still an empty/blank file. So need to check for that as well