C# – Best way to make sure the user dont upload images more than 1MB in size


I am using ASP.net with VB.NET.
Is there some Validator that I can use that will check the size of the uploaded image?
Else what must I do in code to make sure that the user do not upload images more than 1MB?

p.s C# code will also do, I can use a converter to VB.NET


For some reason when I change the maxRequestLength to 1024 (in my WEB.config) and I upload a image with size 1.25mb then I get the Microsoft Error page saying "Internet Explorer cannot display the webpage". And I do have a Try Catch block inside my Submit button. If I remove the maxRequestLength from my Web.config then it works fine.

Best Solution

This is ultimately handled in Web.config. Look for the httpRuntime section:


There are many other settings in httpRuntime but these are the two that are relevant. Here, the maxRequestLength is set to 4096, or 4KB (the number is in bytes). So, set this value accordingly. Also, you will want to set the executionTimeout accordingly as well so it gives a reasonable amount of time to upload whatever you max upload is.

Related Question