Using only JavaScript and framework like dojo/jQuery…
Is there a way to find the Content-Type for a particular file name extension?
Say if I type in the string “something.pdf”, I want to show an alert that says “application/pdf”
or “something.html” show “text/html”?
I think you will need to maintain the mappings yourself (you could XHR a physical file and get it, but I doubt you want to do that)…
jsFiddle.
This will return
falseif it doesn’t exist in your mapping. If you like, you could just return theextToMimes[ext], which will giveundefinedif it doesn’t exist. It also won’t accidentally get things up the prototype chain.There is also the option of accessing it if it is a file
input.Keep in mind the extension to mime type makes no guarantees about the actual file itself; you’d need to parse it server side to determine what kind of file it really is.