Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
74
rated 0 times [  78] [ 4]  / answers: 1 / hits: 45641  / 14 Years ago, mon, january 3, 2011, 12:00:00

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.


More From » jquery

 Answers
16

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.


[#94408] Thursday, December 30, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
isham

Total Points: 69
Total Questions: 86
Total Answers: 86

Location: Anguilla
Member since Sun, Jan 29, 2023
1 Year ago
;