Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery or JavaScript: get MIME type of client file

Tags:

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.

like image 820
SZH Avatar asked Jan 03 '11 00:01

SZH


People also ask

Where is MIME type stored in a file?

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.

What is MIME type in jquery AJAX?

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.


1 Answers

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.

like image 90
Delan Azabani Avatar answered Oct 12 '22 23:10

Delan Azabani