I have file upload UI element in which the user will upload images. Here I have to validate the height and width of the image in client side. Is it possible to find the size of the image having only the file path in JS?
Note: If No, is there any other way to find the dimensions in Client side?
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.