使用文件内容确定Node JS的MIME类型 [英] Use File Content to Determine MIME Type with Node JS

查看:457
本文介绍了使用文件内容确定Node JS的MIME类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎所有流行的node.js MIME类型库都只使用文件扩展名,而不是通过查看文件来确定MIME类型.

It seems all of the popular MIME type libraries for node.js just use the file name extension rather than peeking into the file to determine the MIME type.

在没有扩展名的情况下,是否存在使用Node跳入文件并智能确定文件的MIME类型的好方法?

Is there a good way to use Node to jump into the file and intelligently determine the file's MIME type in case an extension is not present?

推荐答案

这确实有点可惜,大多数流行的 MIME 模块只是将扩展名映射到该类型.

That indeed feels like a pity, that most popular MIME modules are just mapping extension to the type.

深入研究之后,我发现了名为 mmmagic 的模块完全按照自己的意愿做.

After searching deeper, I found the module called mmmagic, it seems to be doing exactly what you want.

请注意,从使用 MIME 进行操作后,我对 MIME 的检测原则上并不完全可靠,并且极少有可能出错检测.

Be aware, that from working with MIME I was left with a taste, that MIME detection is in principle not completely reliable, and there is a rare chance of false detections.

用法示例(摘自网站):

Example of usage (taken from their site):

  var mmm = require('mmmagic'),
      Magic = mmm.Magic;

  var magic = new Magic(mmm.MAGIC_MIME_TYPE);
  magic.detectFile('node_modules/mmmagic/build/Release/magic.node', function(err, result) {
      if (err) throw err;
      console.log(result);
      // output on Windows with 32-bit node:
      //    application/x-dosexec
  });

这篇关于使用文件内容确定Node JS的MIME类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆