最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

internet explorer - How can I determine the mime-type of a document using javascript only? - Stack Overflow

programmeradmin3浏览0评论

I need to determine the mime-type of certain files that get returned from the service, I do not know their file extensions or what their type is before hand, basically we retrieve documents from the API like such: "" 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="" 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 I need to target, "contentDocument" is not defined (apparently I use just "document" instead), but "contentType" is also undefined and I don't know what the IE equivalent would be (google searches have not yielded any results).

Help is greatly appreciated, thanks.

I need to determine the mime-type of certain files that get returned from the service, I do not know their file extensions or what their type is before hand, basically we retrieve documents from the API like such: "http://site./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./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 I need to target, "contentDocument" is not defined (apparently I use just "document" instead), but "contentType" is also undefined and I don't know what the IE equivalent would be (google searches have not yielded any results).

Help is greatly appreciated, thanks.

Share Improve this question asked Jan 27, 2010 at 17:53 FolkenFolken 4572 gold badges7 silver badges13 bronze badges 1
  • Hi did you ever find a nice way to do this? I'm facing the same problem. I am planning on using JavaScript to get the mime type based on the file extension. Did you find/write any function that does this lookup? – Daveo Commented Apr 9, 2010 at 13:43
Add a ment  | 

3 Answers 3

Reset to default 2

If http://site. is different than your domain, you won't be able to access its data on any browser. This is because of Cross-site scripting security measures.

IE supports document.mimeType, but it seems to give a friendly name not the actual foo/bar syntax. Which is wierd. Though this may be good enough for your purposes.

I don't know why it isn't documented here, but the native IHTMLDocument2 interface does document it here. Very strange.

Perhaps:

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

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

Sorry end of day no time to test....

发布评论

评论列表(0)

  1. 暂无评论