views:

458

answers:

3

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?

Thanks in advanced

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......................

+2  A: 

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
Vikram
Yeah, but by default the maximum size I believe is smaller than 1MB so you still have to deal with Web.config.
BobbyShaftoe
+3  A: 

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.

BobbyShaftoe
in the event that the upload is over this size ASP.net wont even process the request however.
Saint Gerbil
@Saint which could be good thing, not getting DDOS'd by someone uploading gigabyte files from hundreds of clients ;)
Chad Grant
I have tried this before but for some reason when i change the maxRequestLength to 1024 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.....
Etienne
You're setting it to 1024? That is only 1KB. 1MB = 1024 * 1024bytes.
BobbyShaftoe
@Bobby, i understand that, but i thought that it would throw some exception and go into my Try Catch block so that i can tell the User that the Image is to large.
Etienne
No, ASP.NET willl not process a page too large. Nevertheless, you do have to set this. There is no much you can do on the client side. You can call a simple ActiveXObject in Javascript to do this but it would only work in IE. The other thing you could do is make the maxRequestLength much larger, maybe 10MB and then you can check the FileUpload1.PostedFile.ContentLength (assuming you're using FileUpload); However, you could just state up front what the the max upload limit is.
BobbyShaftoe
MMM, ai, thanks!
Etienne
A: 

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.

plug

I'm the author of SlickUpload, an ASP.NET upload component that solves this issue, among others. You can set the maxRequestLength in the web.config to the max value you want, and SlickUpload will manage doing the upload out of bounds, validating it, and returning useful error messages to the browser. Check it out and let me know if you have any questions.

Chris Hynes