Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

EDIT

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.

like image 878
Etienne Avatar asked Nov 24 '25 06:11

Etienne


2 Answers

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

<httpRuntime 
 executionTimeout="110" 
 maxRequestLength="4096" 
/>

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.

like image 122
BobbyShaftoe Avatar answered Nov 26 '25 20:11

BobbyShaftoe


You can use the following code to determine the size [in KB] of the uploaded file and once you know the size you can easily decide if you want to proceed further with the file or reject the upload.

Request.Files(0).ContentLength / 1024
like image 20
Vikram Avatar answered Nov 26 '25 19:11

Vikram



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!