javascript node.js 中的自动 mimetypes

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5316324/
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-10-25 16:46:36  来源:igfitidea点击:

automatic mimetypes in javascript node.js

javascriptnode.jsknox-amazon-s3-client

提问by Mark

I'm uploading files to aws s3 in javascript, and AFAIK they all need a mimetype to function correctly. Is there a script that automatically maps filenames to mimetypes that I could use?

我正在用javascript将文件上传到aws s3,而且AFAIK他们都需要一个mimetype才能正常运行。是否有脚本可以自动将文件名映射到我可以使用的 mimetypes?

Edit: If there's a way for amazon to automatically handle the mimetypes that would be better.

编辑:如果亚马逊有办法自动处理 mimetypes 会更好。

回答by Zikes

https://github.com/broofa/node-mimeis a decent looking library for automatic mime lookups.

https://github.com/broofa/node-mime是一个看起来不错的自动 mime 查找库。

回答by Ricardo Tomasi

npm install mime

npm 安装mime

require('mime')

var mimetype = mime.lookup('file.txt')

回答by Philippe Gerber

Found this question via Google and just wanted to add that mime type detection by looking at the file extension is not secure at all. I'd recomment using mmmagicwhich actually looks at the data to determine the mime type of a file.

通过谷歌发现这个问题,只是想通过查看文件扩展名来添加 mime 类型检测根本不安全。我建议使用mmmagic,它实际上查看数据以确定文件的 MIME 类型。