I would like to detect the MIME type of a file on the client side of my application using jQuery or JavaScript. Is there a way to do this? Thanks.
All MIME type information is stored in a database. The MIME database is located in the directory /usr/share/mime/ . The MIME database contains a large number of common MIME types, stored in the file /usr/share/mime/packages/freedesktop. org.
MIME (Multipurpose Internet Mail Extensions) type is a standard way of describing a data type in the body of an HTTP message or email. The MIME type is passed in the Content-Type header. For example, the Content-Type: text/html header tells the browser that it received an HTML page.
You could use AJAX, make a HEAD request, and inspect the response headers for the Content-type
header. But that only works if you're getting a file from a HTTP server.
To get the MIME type of a file chosen with an HTML file chooser, without submitting anything, try:
document.getElementById('fileChooserID').files[0].type // e.g. image/png
Example
http://jsbin.com/akati3/2
Try choosing an image, check the MIME type, and try to submit it. Then try something else that's not an image.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With