Windwalker
Windwalker

Reputation: 1935

Check Browser Support for specific Mime Type?

For a web application which allows in-browser preview of documents, I'd like to check whether the user's browser supports preview of the current document's mime type.

Is there a Javascript-based way to match the current mime type against the types supported by the browser?

Thanks!

Upvotes: 19

Views: 9787

Answers (4)

Aivar
Aivar

Reputation: 2029

If you define which plugin is needed for specific document type, then you may try to look if needed plugin exists. Should work at least on Firefox and Chrome. window.navigator.plugins

Upvotes: 0

In recent browsers there are navigatior.plugins array-like object. You can check each plugin for your mime type.

Here is the solution gist and jsfiddle.

var mimeCheck = function (type) {
    return Array.prototype.reduce.call(navigator.plugins, function (supported, plugin) {
        return supported || Array.prototype.reduce.call(plugin, function (supported, mime) {
            return supported || mime.type == type;
        }, supported);
    }, false);
};

Upvotes: 4

Fez Vrasta
Fez Vrasta

Reputation: 14815

In this question there was the same question I think, try to check out it

Check if a browser supports a specific MIME type?

Upvotes: 1

Timmetje
Timmetje

Reputation: 7694

You could make an AJAX call and check response headers for mimetype.

 $.ajax({
    type: "GET",
    url: 'http://..../thing.pdf',
    success: function (output, status, xhr) {
      alert("done!"+ xhr.getAllResponseHeaders());
      alert("done!"+ xhr.getResponseHeader("Content-Type"));
    }
  });

Upvotes: 2

Related Questions