Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the default file extension for a content-type in JavaScript?

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.

like image 679
Senseful Avatar asked Jan 10 '12 09:01

Senseful


People also ask

How do I find the MIME type of a file?

For detecting MIME-types, use the aptly named "mimetype" command. It has a number of options for formatting the output, it even has an option for backward compatibility to "file". But most of all, it accepts input not only as file, but also via stdin/pipe, so you can avoid temporary files when processing streams.

What is the extension of a type script file?

TypeScript is another file format that uses the . TS file extension. These are text files used to make JavaScript applications and are in fact similar to JavaScript (.

What is the default extension?

Extension Default means the occurrence of any Event of Default or any event that with the giving of notice or the lapse of time could become an Event of Default other than the failure to pay the principal balance of the Bridge Loans or the Conversion Notes on the Initial Maturity Date.


1 Answers

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');
like image 101
Chris Peacock Avatar answered Oct 11 '22 01:10

Chris Peacock