I am uploading images to a servlet. The validation whether the uploaded file is an image is done in server side only, by checking the magic numbers in the file header. Is there any way to validate the extensions in client side before submitting the form to servlet? As soon as I hit enter it starts uploading.
I am using Javascript and jQuery in client side.
Update: I was finally ended up with server side validation which reads bytes & rejects the upload if it is not an image.
Do you use the input type="file" to choose the uploadfiles? if so, why not use the accept attribute?
try this (Works for me)
If you're needing to test remote urls in an input field, you can try testing a simple regex with the types that you're interested in.
This will capture anything ending in .gif, .jpg, .jpeg, .tiff or .png
I should note that some popular sites like Twitter append a size attribute to the end of their images. For instance, the following would fail this test even though it's a valid image type:
Because of that, this isn't a perfect solution. But it will get you to about 90% of the way.
You can use the
accept
attribute available for input file types. Checkout MDN documentationI came here because I was sure none of the answers here were quite...poetic: