Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

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?
0
Camillia
Asked:
Camillia
  • 2
  • 2
1 Solution
 
Shaun KlineLead 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

Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now