Share via

Checking file size in FileUpload

Donald Symmons 3,066 Reputation points
May 9, 2023, 7:53 AM

Hello community,

Is this how to limit file size for two FileUpload controls?

if (FileUpload1.PostedFile.ContentLength < 102400 & Filedoc.PostedFile.ContentLength < 102400) 
{ 
string filePath = Path.GetFileName(FileUpload1.PostedFile.FileName);
string File = Path.GetFileName(Filedoc.PostedFile.FileName); 
Label1.Text = “Image Uploaded Successfully !!”; 
} 
else 
{ 
Label1.Text = “Image1 must be less than 600kb”; 
Label2.Text = "Image2 must be less than 400kb";
}


And how can I make it to accept file size of not more than 600kb for the first FileUpload control and accept size of not more than 300kb in the second fileupload?

Thank you

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,103 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,598 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,404 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 30,181 Reputation points Microsoft External Staff
    May 9, 2023, 9:16 AM

    Hi @Donald Symmons,

    Is this how to limit file size for two FileUpload controls?

    Yes.

    PostedFile.ContentLength is in bytes.

    600kb=600000 Bytes

    300kb=300000 Bytes

     if (FileUpload1.PostedFile.ContentLength < 600000 && Filedoc.PostedFile.ContentLength < 300000)
                {
                    string filePath = Path.GetFileName(FileUpload1.PostedFile.FileName);
                    string File = Path.GetFileName(Filedoc.PostedFile.FileName);
                    Label1.Text = "Image Uploaded Successfully!!";
                }
                else if(FileUpload1.PostedFile.ContentLength >= 600000)
                {
                    Label1.Text = "Image1 must be less than 600kb";
                   
                }
                else if (Filedoc.PostedFile.ContentLength >= 300000)
                {
                    Label2.Text = "Image2 must be less than 300kb";
                }
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.