Alessandro Minneci Alessandro Minneci - 3 months ago 10
ASP.NET (C#) Question

How to set max FileSize of uploaded file with if-statement?

I created an

<asp:fileupload id="FileUploadPassfoto">
on my webpage. (ASP.NET AND C#)

I want to check if the uploaded file(image) is bigger than 6000KB. If true, the file wont be accepted and a error message will be displayed. I stored the uploaded file as following: (CODE BEHIND)

System.Drawing.Image photo = System.Drawing.Image.FromStream(FileUploadPassfoto.PostedFile.InputStream);


Now i want to check if the image is bigger then 6000KB.

It works with something like photo.size > Size(6000KB).

Any suggestions?

Div Div
Answer

You can check like this:

decimal size = Math.Round(((decimal)FileUploadPassfoto.PostedFile.ContentLength / (decimal)1024), 2);
    if(size > 6000)
    {
       // size is greater than 6000KB 
    }
Comments