2

I am currently working on my asp.net project. What I have to do is, user must upload image that should have size less than 20KB. I want to use javascript or jquery. Is there any way to restrict the image size.

Please help me out. I am just stuck at this point.

Thanks

Adeel Kamal
  • 131
  • 1
  • 1
  • 8

3 Answers3

2

Javascript does not have access to the file user selects so its out of the question.

You can restrict size on server side or by using flash/silverlight for the upload.

Simon
  • 2,449
  • 2
  • 18
  • 20
  • thanks simon, is there any other client side way to restrict image upload size.? – Adeel Kamal Feb 24 '12 at 14:41
  • Most frameworks that runs in the browser as flash, javaapplet, silverlight, activexcontroll could be used. But flash/silver is most common. Perhaps also html5?? I've not familiarized myself with it yet. – Simon Feb 24 '12 at 14:46
  • We just built a flash one, still a bit buggy http://www.saljnet.se/create/ (click on "välj bilder") – Simon Feb 24 '12 at 14:53
  • html-5 may do the trick. I did this recently and it works good. You need to get a hold of the file object and can then check the size etc before uploading. – terjeto Feb 24 '12 at 14:55
1

In your Web.Config under System.Web, write below lines.

 <httpRuntime maxRequestLength="Your amount for 20 KB"/>

The maximum request size in kilobytes. The default size is 4096 KB (4 MB).

Pankaj
  • 9,749
  • 32
  • 139
  • 283
  • Yeah but remember this will be applied to the whole site (or at least to the scope of the folder containing the config). That could cause other requests which are larger than his image restriction to stop working. This is basically a precaution to stop DOS attacks. I would not recommend this approach to solve this one issue. – swannee Feb 24 '12 at 14:46
  • 1
    20KB is pretty small. Any request posted in that folder would have to be under 20KB. And the runtime will just raise an error instead of allowing the developer to send a nice message back to the user telling them the file is too large. It's just not the right tool for this job. – swannee Feb 24 '12 at 14:55
  • what this attribute 'maxRequestLength' mean in httpRuntime tag in web.config infact.? I just want to restrict the upload image size not the request size. – Adeel Kamal Feb 24 '12 at 14:58
  • @Adeel Kamal: This is **not** a recommended technique. It will restrict the entire request size of **any** request scoped to that config file, not just images. If the threshold is exceeded asp.net will simply return a 404 (page not found) which cannot be easily handled by the app to respond to the user. The browser will see a standard "page cannot be displayed". Again, this is to handle DOS (Denial of Service) attacks, not as a validation mechanism. – swannee Feb 24 '12 at 16:41
0

There are some JQuery plugins to help with this. Why reinvent the wheel right? Here's a link to the plugins page for file upload. Some are specific to images:

http://archive.plugins.jquery.com/plugin-tags/upload

It looks like restricting the file size in Javascript is an issue. Here's a related question where this is discussed at length:

Using jQuery, Restricting File Size Before Uploading

Community
  • 1
  • 1
swannee
  • 3,346
  • 2
  • 24
  • 40