Javascript Node.js - 文件系统获取文件类型,2012 年左右的解决方案

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10431845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 01:16:50  来源:igfitidea点击:

Node.js - File System get file type, solution around year 2012

javascriptnode.jsfilesystemsfile-type

提问by noob

I need to get the file type of a file with the help of node.js to set the content type. I know I can easily check the file extension but I've also got files without extension which should have the content type image/png, text/htmlaso.

我需要在 node.js 的帮助下获取文件的文件类型来设置内容类型。我知道我可以轻松检查文件扩展名,但我也有没有扩展名的文件,这些文件应该具有内容类型image/pngtext/htmlaso。

This is my code (I know it doesn't make much sense but that's the base I need):

这是我的代码(我知道它没有多大意义,但这是我需要的基础):

var http = require("http"),
    fs = require("fs");
http.createServer(function(req, res) {
    var data = "";
    try {
        /*
         * Do not use this code!
         * It's not async and it has a security issue.
         * The code style is also bad.
         */
        data = fs.readFileSync("/home/path/to/folder" + req.url);
        var type = "???"; // how to get the file type??
        res.writeHead(200, {"Content-Type": type});
    } catch(e) {
        data = "404 Not Found";
        res.writeHead(404, {"Content-Type": "text/plain"});
    }
    res.write(data);
    res.end();
}).listen(7000);

I haven't found a function for that in the APIso I would be happy if anyone can tell me how to do it.

我还没有在API 中找到一个函数,所以如果有人能告诉我怎么做,我会很高兴。

采纳答案by ThiefMaster

Have a look at the mmmagic module. It is a libmagic binding and seems to do exactly what you want.

看看mmmagic 模块。它是一个 libmagic 绑定,似乎完全符合您的要求。

回答by 250R

There is a helper library for looking up mime types https://github.com/broofa/node-mime

有一个帮助库用于查找 mime 类型https://github.com/broofa/node-mime

var mime = require('mime');

mime.getType('/path/to/file.txt');         // => 'text/plain'

But it still uses the extension for lookup though

但它仍然使用扩展名进行查找

回答by Daniel Baulig

You should have a look at the command line tool file(Linux). It attempts to guess the filetype based on the first couple of bytes of the file. You can use child_process.spawnto run it from within node.

你应该看看命令行工具file(Linux)。它尝试根据文件的前几个字节猜测文件类型。您可以使用child_process.spawn从节点内运行它。

回答by timoxley

You want to be looking up the mime type, and thankfully node has a handy library just for that:

你想查找 mime 类型,幸好 node 有一个方便的库:

https://github.com/bentomas/node-mime#readme

https://github.com/bentomas/node-mime#readme

edit:

编辑:

You should probably look into a static asset server and not be setting any of this stuff yourself. You can use expressto do this really easily, or there's a whole host of static file modules, e.g. ecstatic. On the other hand you should probably use nginx to serve static files anyway.

您可能应该查看静态资产服务器,而不是自己设置任何这些东西。您可以使用express非常轻松地完成此操作,或者有大量静态文件模块,例如ecstatic。另一方面,您可能应该使用 nginx 来提供静态文件。

回答by Stephen Paul

2018 solution

2018年解决方案

The accepted answer appears to have a Python dependency and the other answers are either out-of-date or presume the file name has some sort of extension.

接受的答案似乎具有 Python 依赖性,而其他答案要么已过时,要么假定文件名具有某种扩展名。

Please find my more up-to-date answer here

在此处找到我的更多最新答案

回答by Andrea Girardi

I used this:

我用过这个:

npm install mime-types

And, inside the code:

而且,在代码中:

var mime = require('mime-types');
tmpImg.contentType = mime.lookup(fileImageTmp);

Where fileImageTmp is the copy of image stored on the file system (in this case tmp).

其中 fileImageTmp 是存储在文件系统上的图像副本(在本例中为 tmp)。

The result I can see is: image/jpeg

我能看到的结果是:image/jpeg