Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

The cannot display web page error occurs because ASP.NET breaks the connection for oversized requests to mitigate DOS attacks based on oversized requests. To get around this, you'd have to do the upload in a iframe and then detect whether an error occurred or not. You could also use a flash, silverlight, java, or activex uploader component installed on the client to validate the file size client side, but that will require installation depending on your solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top