如何从文件扩展名中获得MIME类型?
当前回答
FileExtension处理文件扩展名,而不是Mime。用户可以更改文件扩展名,因此请检查Mime。代码示例通过文件扩展名关联Mime,这是错误的,不能工作。
需要获取内容类型文件,并检查表Mime contetType该文件根据文件扩展名。现在,如何获得文件的内容类型?
使用FileUpload是这样的: 现在,如果我已经有文件,捕捉你的ContentType?
其他回答
您可以在注册表中找到这些信息。例如,.pdf文件的MIME类型可以在键HKEY_CLASSES_ROOT\.pdf中找到,在值"Content type "中:
string mimeType = Registry.GetValue(@"HKEY_CLASSES_ROOT\.pdf", "Content Type", null) as string;
IANA媒体类型
我希望微软能把他们的行业标准统一起来!对于其他人,有兴趣的人:
离散的类型
application: https://www.iana.org/assignments/media-types/media-types.xhtml#application audio: https://www.iana.org/assignments/media-types/media-types.xhtml#audio example: https://www.iana.org/assignments/media-types/media-types.xhtml#examples font: https://www.iana.org/assignments/media-types/media-types.xhtml#font image: https://www.iana.org/assignments/media-types/media-types.xhtml#image model: https://www.iana.org/assignments/media-types/media-types.xhtml#model text: https://www.iana.org/assignments/media-types/media-types.xhtml#text video: https://www.iana.org/assignments/media-types/media-types.xhtml#video
多部分类型
信息:https://www.iana.org/assignments/media-types/media-types.xhtml消息 多部分:https://www.iana.org/assignments/media-types/media-types.xhtml #多部分
我想推荐一个阅读:MIME类型(IANA媒体类型)的Mozilla页面感兴趣的人!它的信息量很大!
在代码方面,上面的每个链接都有一个.csv文件下载:https://www.iana.org/assignments/media-types/application.csv
正如这里已经指出的,可以使用Dictionary或ConcurrentDictionary下载并使用Key - Value对填充Dictionary。
受到塞缪尔回答的启发,我写了一个改进版本:
当扩展名是大写时也适用。 以文件名为输入,优雅地处理没有扩展名的文件。 不要在键中包含“。”。 列表,为此我编写了一个小型转换脚本。
最终的源代码超过30K个字符,所以我不能张贴在这里,在Github上检查它。
为了让Shimmy的回答更清楚:
var mimeType = MimeMapping.GetMimeMapping(fileName);
System.Web.dll v4.5开发
// Summary:
// Returns the MIME mapping for the specified file name.
//
// Parameters:
// fileName:
// The file name that is used to determine the MIME type.
public static string GetMimeMapping(string fileName);
您不应该信任来自客户端的文件扩展名。总是检查文件的神奇数字。
使用filetpe讯问器与ASP。NET核心:
public static class FileTypeChecker
{
private static List<string> validVideoMimeTypes = new List<string> { "video/mp4", "video/quicktime" };
private static List<string> validImageMimeTypes = new List<string> { "image/png", "image/jpeg" };
public static bool IsValidVideo(IFormFile file)
{
return validVideoMimeTypes.Contains(GetFileMimeType(file));
}
public static bool IsValidImage(IFormFile file)
{
return validImageMimeTypes.Contains(GetFileMimeType(file));
}
private static string GetFileMimeType(IFormFile file)
{
// You should have checked for null and file length before reaching here
IFileTypeInterrogator interrogator = new FileTypeInterrogator.FileTypeInterrogator();
byte[] fileBytes;
using (var stream = new MemoryStream())
{
file.CopyTo(stream);
fileBytes = stream.ToArray();
}
FileTypeInfo fileTypeInfo = interrogator.DetectType(fileBytes);
return fileTypeInfo.MimeType.ToLower();
}
}
在你的控制器或服务内部:
public IActionResult UploadVideo([FromForm] UploadVideoVM model)
{
if (model.File.Length < minimumLength || model.File.Length > maximumLength)
{
// BadRequest => Size
}
else if (!FileTypeChecker.IsValidVideo(model.File))
{
// BadRequest => Type
}
else
{
// All good
}
return Ok();
}
要获得文件扩展名的MIME类型,请参考此文件。
推荐文章
- 如何强制LINQ Sum()返回0而源集合是空的
- 将值附加到查询字符串
- Selenium c# WebDriver:等待元素出现
- 我如何添加双引号的字符串,是在一个变量?
- 检查字符串是否包含字符串列表中的元素
- 最好的方法在asp.net强制https为整个网站?
- 在ASP .NET身份声明是什么
- 将字符串转换为System.IO.Stream
- 如何从枚举中选择一个随机值?
- 驻留在App_Code中的类不可访问
- 在链式LINQ扩展方法调用中等价于'let'关键字的代码
- dynamic (c# 4)和var之间的区别是什么?
- Visual Studio: ContextSwitchDeadlock
- 返回文件在ASP。Net Core Web API
- 自定义HttpClient请求头