Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 689
Windwalker Avatar asked Feb 12 '13 14:02

Windwalker


People also ask

How does browser determine MIME type?

A browser usually identifies a resource's MIME type by observing the Content-Type response header in an HTTP response. Sometimes, developers set values for Content-Type headers that are not appropriate for the response's content.

How would you determine the MIME type of a file?

The application uses file content sniffers to search for a particular pattern in the file. A file content sniffer associates a specific pattern in a file with a MIME type. If the application finds a match for the pattern, the MIME type associated with the pattern is the MIME type of the file.

How do I find the MIME type of a URL?

Use the guess_type() function to guess the MIME type based on a filename or URL. A tuple of (type, encoding) is returned. The first element of the tuple, type , is the MIME type. The second element, encoding , is a value if the file is compressed with gzip , for example.

Is MIME used in web browsing?

MIME stands for "Multipurpose Internet Mail Extensions". The concept of MIME Types was originally created to be used with email programs; hence the "Mail" part of the acronym. MIME Types are now also used by web browsers.


1 Answers

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);
};
like image 163
Тёма Пендюрин Avatar answered Sep 21 '22 07:09

Тёма Пендюрин