LA_
LA_

Reputation: 20409

How to get mime type of the file before downloading it?

It there any way to identify the mime type of file before downloading it (when only url is known)? For ex., if I would like to show my context menu item only for some particular types. Or, download screen should be changed depending on the download type.

Upvotes: 0

Views: 2195

Answers (1)

Wladimir Palant
Wladimir Palant

Reputation: 57651

You can get the MIME type from file extension using nsIMIMEService.getTypeFromURI(), it will get the MIME type from the file extension. Along these lines:

var uri = Components.classes["@mozilla.org/network/io-service;1"]
                    .getService(Components.interfaces.nsIIOService)
                    .newURI("http://example.com/test.gif", null, null);
var mimeService = Components.classes["@mozilla.org/mime;1"]
                            .getService(Components.interfaces.nsIMIMEService);
var mime = mimeService.getTypeFromURI(uri);

Of course that isn't anything close to reliable - the MIME type of a URL can be anything, text.gif can be an HTML page or an SVG image or anything else. So the only real way to get the MIME type is to start the download.

Upvotes: 1

Related Questions