Reputation: 91641
If I have a content-type, how can I get its default file extension?
For example, given one of these content-types, I should get the following file extensions:
+------------------------+----------------+
| Content-Type | File Extension |
+------------------------+----------------+
| text/plain | .txt |
| application/javascript | .js |
| application/gzip | .gz |
+------------------------+----------------+
If there is no way to get these extensions automatically in JavaScript, my next solution would be to create a simple relationship between content-type and file extension in a JavaScript array. Therefore, I would accept an answer which has a simple table/database of the most common content-type and file extensions.
Upvotes: 36
Views: 51243
Reputation: 38106
If you want to roll your own MIME -> extension mapping (rather than using a package), this list would be a good start: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
The full list of all official MIME types (linked from the article above) can be found here: https://www.iana.org/assignments/media-types/media-types.xhtml
Upvotes: 0
Reputation: 624
export const FileMimeType = {
'audio/x-mpeg': 'mpega',
'application/postscript': 'ps',
'audio/x-aiff': 'aiff',
'application/x-aim': 'aim',
'image/x-jg': 'art',
'video/x-ms-asf': 'asx',
'audio/basic': 'ulw',
'video/x-msvideo': 'avi',
'video/x-rad-screenplay': 'avx',
'application/x-bcpio': 'bcpio',
'application/octet-stream': 'exe',
'image/bmp': 'dib',
'text/html': 'html',
'application/x-cdf': 'cdf',
'application/pkix-cert': 'cer',
'application/java': 'class',
'application/x-cpio': 'cpio',
'application/x-csh': 'csh',
'text/css': 'css',
'application/msword': 'doc',
'application/xml-dtd': 'dtd',
'video/x-dv': 'dv',
'application/x-dvi': 'dvi',
'application/vnd.ms-fontobject': 'eot',
'text/x-setext': 'etx',
'image/gif': 'gif',
'application/x-gtar': 'gtar',
'application/x-gzip': 'gz',
'application/x-hdf': 'hdf',
'application/mac-binhex40': 'hqx',
'text/x-component': 'htc',
'image/ief': 'ief',
'text/vnd.sun.j2me.app-descriptor': 'jad',
'application/java-archive': 'jar',
'text/x-java-source': 'java',
'application/x-java-jnlp-file': 'jnlp',
'image/jpeg': 'jpg',
'application/javascript': 'js',
'text/plain': 'txt',
'application/json': 'json',
'audio/midi': 'midi',
'application/x-latex': 'latex',
'audio/x-mpegurl': 'm3u',
'image/x-macpaint': 'pnt',
'text/troff': 'tr',
'application/mathml+xml': 'mathml',
'application/x-mif': 'mif',
'video/quicktime': 'qt',
'video/x-sgi-movie': 'movie',
'audio/mpeg': 'mpa',
'video/mp4': 'mp4',
'video/mpeg': 'mpg',
'video/mpeg2': 'mpv2',
'application/x-wais-source': 'src',
'application/x-netcdf': 'nc',
'application/oda': 'oda',
'application/vnd.oasis.opendocument.database': 'odb',
'application/vnd.oasis.opendocument.chart': 'odc',
'application/vnd.oasis.opendocument.formula': 'odf',
'application/vnd.oasis.opendocument.graphics': 'odg',
'application/vnd.oasis.opendocument.image': 'odi',
'application/vnd.oasis.opendocument.text-master': 'odm',
'application/vnd.oasis.opendocument.presentation': 'odp',
'application/vnd.oasis.opendocument.spreadsheet': 'ods',
'application/vnd.oasis.opendocument.text': 'odt',
'application/vnd.oasis.opendocument.graphics-template': 'otg',
'application/vnd.oasis.opendocument.text-web': 'oth',
'application/vnd.oasis.opendocument.presentation-template': 'otp',
'application/vnd.oasis.opendocument.spreadsheet-template': 'ots',
'application/vnd.oasis.opendocument.text-template': 'ott',
'application/ogg': 'ogx',
'video/ogg': 'ogv',
'audio/ogg': 'spx',
'application/x-font-opentype': 'otf',
'audio/flac': 'flac',
'application/annodex': 'anx',
'audio/annodex': 'axa',
'video/annodex': 'axv',
'application/xspf+xml': 'xspf',
'image/x-portable-bitmap': 'pbm',
'image/pict': 'pict',
'application/pdf': 'pdf',
'image/x-portable-graymap': 'pgm',
'audio/x-scpls': 'pls',
'image/png': 'png',
'image/x-portable-anymap': 'pnm',
'image/x-portable-pixmap': 'ppm',
'application/vnd.ms-powerpoint': 'pps',
'image/vnd.adobe.photoshop': 'psd',
'image/x-quicktime': 'qtif',
'image/x-cmu-raster': 'ras',
'application/rdf+xml': 'rdf',
'image/x-rgb': 'rgb',
'application/vnd.rn-realmedia': 'rm',
'application/rtf': 'rtf',
'text/richtext': 'rtx',
'application/font-sfnt': 'sfnt',
'application/x-sh': 'sh',
'application/x-shar': 'shar',
'application/x-stuffit': 'sit',
'application/x-sv4cpio': 'sv4cpio',
'application/x-sv4crc': 'sv4crc',
'image/svg+xml': 'svgz',
'application/x-shockwave-flash': 'swf',
'application/x-tar': 'tar',
'application/x-tcl': 'tcl',
'application/x-tex': 'tex',
'application/x-texinfo': 'texinfo',
'image/tiff': 'tiff',
'text/tab-separated-values': 'tsv',
'application/x-font-ttf': 'ttf',
'application/x-ustar': 'ustar',
'application/voicexml+xml': 'vxml',
'image/x-xbitmap': 'xbm',
'application/xhtml+xml': 'xhtml',
'application/vnd.ms-excel': 'xls',
'application/xml': 'xsl',
'image/x-xpixmap': 'xpm',
'application/xslt+xml': 'xslt',
'application/vnd.mozilla.xul+xml': 'xul',
'image/x-xwindowdump': 'xwd',
'application/vnd.visio': 'vsd',
'audio/x-wav': 'wav',
'image/vnd.wap.wbmp': 'wbmp',
'text/vnd.wap.wml': 'wml',
'application/vnd.wap.wmlc': 'wmlc',
'text/vnd.wap.wmlsc': 'wmls',
'application/vnd.wap.wmlscriptc': 'wmlscriptc',
'video/x-ms-wmv': 'wmv',
'application/font-woff': 'woff',
'application/font-woff2': 'woff2',
'model/vrml': 'wrl',
'application/wspolicy+xml': 'wspolicy',
'application/x-compress': 'z',
'application/zip': 'zip'
};
to use this provide mime type to object
console.log(FileMimeType[image/jpeg])
Upvotes: 7
Reputation: 10877
There is a best package is
var mimeDb = require("mime-db")
usage(it return object)
var data = mimeDb['image/x-icon']
console.log(data)
To get extenstions
console.log(data.extensions[0])
another one is mime-types derived from mime-db
but while i use mime types little bugy in getting extension image/x-icon
Upvotes: 13
Reputation:
from nginx
types {
text/html html htm shtml;
text/css css;
text/xml xml;
image/gif gif;
image/jpeg jpeg jpg;
application/x-javascript js;
application/atom+xml atom;
application/rss+xml rss;
text/mathml mml;
text/plain txt;
text/vnd.sun.j2me.app-descriptor jad;
text/vnd.wap.wml wml;
text/x-component htc;
image/png png;
image/tiff tif tiff;
image/vnd.wap.wbmp wbmp;
image/x-icon ico;
image/x-jng jng;
image/x-ms-bmp bmp;
image/svg+xml svg;
image/webp webp;
application/java-archive jar war ear;
application/mac-binhex40 hqx;
application/msword doc;
application/pdf pdf;
application/postscript ps eps ai;
application/rtf rtf;
application/vnd.ms-excel xls;
application/vnd.ms-powerpoint ppt;
application/vnd.wap.wmlc wmlc;
application/vnd.google-earth.kml+xml kml;
application/vnd.google-earth.kmz kmz;
application/x-7z-compressed 7z;
application/x-cocoa cco;
application/x-java-archive-diff jardiff;
application/x-java-jnlp-file jnlp;
application/x-makeself run;
application/x-perl pl pm;
application/x-pilot prc pdb;
application/x-rar-compressed rar;
application/x-redhat-package-manager rpm;
application/x-sea sea;
application/x-shockwave-flash swf;
application/x-stuffit sit;
application/x-tcl tcl tk;
application/x-x509-ca-cert der pem crt;
application/x-xpinstall xpi;
application/xhtml+xml xhtml;
application/zip zip;
application/octet-stream bin exe dll;
application/octet-stream deb;
application/octet-stream dmg;
application/octet-stream eot;
application/octet-stream iso img;
application/octet-stream msi msp msm;
audio/midi mid midi kar;
audio/mpeg mp3;
audio/ogg ogg;
audio/x-realaudio ra;
video/3gpp 3gpp 3gp;
video/mpeg mpeg mpg;
video/quicktime mov;
video/x-flv flv;
video/x-mng mng;
video/x-ms-asf asx asf;
video/x-ms-wmv wmv;
video/x-msvideo avi;
video/mp4 m4v mp4;
}
Upvotes: 18
Reputation: 4686
For Node applications, there is a more up-to-date answer now, which is to use the mime-types NPM package, e.g.:
var mime = require('mime-types'),
fileExtension = mime.extension('text/plain');
Upvotes: 46