How can I determine the mime type of a document using javascript only?

I need to determine the mime type of certain files that are returned from the service, I do not know their file extensions or what their type is at hand, basically we extract documents from the API, for example: " http://site.com/getFile/ BFD843DFLKXJDF ", where the random text at the end is some file.

<input type="button" value="Click Me" onClick="alert(document.getElementById('iframeID').contentDocument.contentType);">

<iframe id="iframeID" src="http://site.com/getFile/BFD843DFLKXJDF" width="600" height="600" />

This works in Firefox, it returns in the alert dialog: "application / pdf" for PDF files. However, Internet Explorer is the main browser that I need to configure, the “contentDocument” is not defined (apparently I use only the “document” instead), but the “contentType” is also undefined, and I don’t know what the IE equivalent is (Google searches yielded no results).

Help is much appreciated, thanks.

+5
source share
3 answers

If http://site.com is different from your domain, you will not be able to access its data in any browser. This is due to cross-site scripting security measures.

+1

:

var frame = document.getElementById('iframeID');  
var type;  

if (frame.contentDocument) { 
  type = frame.contentDocument.contentType;  
} else {
  type = frame.contentWindow.document.contentType;  
}
alert(type);  

, ....

0

IE supports document.mimeTypebut seems to give a friendly name not the actual foo / bar syntax. Which is strange. Although it may be good enough for your purposes.

I do not know why this is not documented here , but the IHTMLDocument2 native interface documents it here . Very strange.

0
source

All Articles