Sunday, 18 August 2013

How to check file MIME type with javascript before upload?

How to check file MIME type with javascript before upload?

I have read this and this questions which seems to suggest that the file
MIME type could be checked using javascript on client side. Now, I
understand that the real validation still has to be done on server side. I
want to perform a client side checking to avoid unnecessary wastage of
server resource.
To test whether this can be done on client side, I changed the extension
of a JPEG test file to .png and choose the file for upload. Before sending
the file, I query the file object using a javascript console:
document.getElementsByTagName('input')[0].files[0];
This is what I get on Chrome 28.0:
File {webkitRelativePath: "", lastModifiedDate: Tue Oct 16 2012 10:00:00
GMT+0000 (UTC), name: "test.png", type: "image/png", size: 500055…}
It shows type to be image/png which seems to indicate that the checking is
done based on file extension instead of MIME type. I tried Firefox 22.0
and it gives me the same result. But according to the W3C spec, MIME
Sniffing should be implemented.
Am I right to say that there is no way to check the MIME type with
javascript at the moment? Or am I missing something?

No comments:

Post a Comment