Checking file size in FileUpload

Donald Symmons 3,066 Reputation points
2023-05-09T07:53:41.2433333+00:00

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

Developer technologies .NET Other
Developer technologies ASP.NET Other
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2023-05-09T09:16:00.7166667+00:00

    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.