C# – How to check file size on upload

asp.netc

Whats the best way to check the size of a file during upload using asp.net and C#? I can upload large files by altering my web.config without any problems. My issues arises when a file is uploaded which is more than my allowed max file size.

I have looked into using activex objects but that is not cross browser compatible and not the best answer to the solution. I need it to be cross browser compatible if possible and to support IE6 (i know what you are thinking!! However 80% of my apps users are IE6 and this is not going to change anytime soon unfortunately).

Has any dev out there come across the same problem? And if so how did you solve it?

Best Answer

If you are using System.Web.UI.WebControls.FileUpload control:

MyFileUploadControl.PostedFile.ContentLength;

Returns the size of the posted file, in bytes.