开发者

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

开发者 https://www.devze.com 2022-12-18 02:24 出处:网络
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 A

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.com/getFile/BFD843DFLKXJDF" where the random text at the end is some file.

<input type="button" value="Click Me" onClick="alert(document.getElementById('iframeID').conten开发者_StackOverflow社区tDocument.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 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.


If http://site.com 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

精彩评论

暂无评论...
验证码 换一张
取 消