I am using ASP.NET C# 4.0, my web form consist of input type file to upload images.
i need to validate the image on client side, before saving it to my SQL database
since i am using input type= file to upload images, i do not want to post back the page for validating the image size, dimensions.
Needs your assistance
Thanks
you can do something like this…
You can do this on browsers that support the new File API from the W3C, using the
readAsDataURLfunction on theFileReaderinterface and assigning the data URL to thesrcof animg(after which you can read theheightandwidthof the image). Currently Firefox 3.6 supports the File API, and I think Chrome and Safari either already do or are about to.So your logic during the transitional phase would be something like this:
Detect whether the browser supports the File API (which is easy:
if (typeof window.FileReader === 'function')).If it does, great, read the data locally and insert it in an image to find the dimensions.
If not, upload the file to the server (probably submitting the form from an iframe to avoid leaving the page), and then poll the server asking how big the image is (or just asking for the uploaded image, if you prefer).
Edit I’ve been meaning to work up an example of the File API for some time; here’s one:
Works great on Firefox 3.6. I avoided using any library there, so apologies for the attribute (DOM0) style event handlers and such.