Limit file to i MegaByte

I have a file upload page and I want to limit the file size to 1 MegaByte. I did this but looks like it's not correct because i have a file of 2404KB that I cant upload. CurrentLength is in Bytes
 if (file.ContentLength > 1048576)
                {
                   lmsg.Text = "Maximum file size is 1 MegaByte";
                   return;
                }


What value should I use?
LVL 7
CamilliaAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Shaun KlineConnect With a Mentor Lead Software EngineerCommented:
A megabyte is 1024 kilobytes which is 1048576 bytes. Your algorithm is working correctly. If you want allow 2404 kilobytes, you will need to change 1048576 to 2461696.
0
 
CamilliaAuthor Commented:
Is there a "standard" file size in ASP.Net that should be allowed for a user to upload? or is it all depends on the application/business logic?
0
 
Shaun KlineLead Software EngineerCommented:
There is no standard. It is as you said, dependent on the business logic/need.
0
 
Kumaraswamy RCommented:
try

long[] numbersToConvert = { 162345, 32183, -54000, Int64.MaxValue/2 };
int newNumber;
foreach (long number in numbersToConvert)
{
   if (number >= Int32.MinValue && number <= Int32.MaxValue)
   {
      newNumber = Convert.ToInt32(number);
      Responce.write("Successfully converted {0} to an Int32.",
                        newNumber);
   }
   else
   {
      Responce.write("Unable to convert {0} to an Int32.", number);
   }
}
0
 
CamilliaAuthor Commented:
What does that code do?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.