views:

429

answers:

3

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').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 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.

+1  A: 

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.

Luca Matteis
site.com is the same domain, but I only have access to the front-end side of things. So I suppose cross-site would not be an issue.
Folken
then why are you using JavaScript to get the content type? shouldn't the server give you that sort of information in a cleaner way?
Luca Matteis
I currently don't have access to back-end code/functionality, so this is why I'm looking for a front-end way to grab the mime-type, if there is a way that the server exposes the mime-type naturally to the front-end then I'd like to hear it 'cause right now I'm at a dead-end.
Folken
A: 

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....

James
Unfortunately not, again it's the contentType property -- if you enter the following javascript into the address bar for this page: "javascript: alert(document.contentType);" and then press "Enter" - in IE you'll get "undefined", in firefox you'll get "text/html".
Folken
A: 

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.

jeffamaphone
Unfortunately I have had no luck using the mimeType property.
Folken
It didn't work at all (script error) or it didn't give you what you wanted?
jeffamaphone
It didn't give the mime-type of the actual document coming in. Case in point, calling document.mimeType on this page gives me "Firefox document" (I assume this means it's an html document -- so far so good), calling it on a JPEG gives me JPG image, also good, calling it on a pdf gives me an error: "Internet Explorer cannot download. Unspecified Error" -- many of the docs we get are in different formats including these.
Folken
Huh. Strange stuff.
jeffamaphone