本文翻译自:Get MIME type from filename extension

如何从文件扩展名中获取MIME类型?


#1楼

参考:https://stackoom.com/question/4Jsi/从文件扩展名获取MIME类型


#2楼

You can find that information in the registry. 您可以在注册表中找到该信息。 For instance, the MIME type for .pdf file can be found in the key HKEY_CLASSES_ROOT\\.pdf , in the value "Content Type" : 例如,.pdf文件的MIME类型可以在密钥HKEY_CLASSES_ROOT\\.pdf中找到,值为“内容类型”:

string mimeType = Registry.GetValue(@"HKEY_CLASSES_ROOT\.pdf", "Content Type", null) as string;

#3楼

You could use the table provided from Apache 's httpd. 你可以使用Apache的httpd提供的表。 It should be trivial to map this into a function, dictionary, list, etc. 将它映射到函数,字典,列表等应该是微不足道的。

Also, as seen here , extension->mime type is not necessarily a function. 此外,由于看到这里 ,扩展- > MIME类型不一定是功能。 There may be multiple common MIME types per file extension, so you should look at the requirements of your application, and see why you care about MIME types, what you want "to do" with them, etc. Can you use file extensions to key the same behavior? 每个文件扩展名可能有多种常见的MIME类型,因此您应该查看应用程序的要求,并了解您关心MIME类型的原因,您希望与它们“做什么”等等。您可以使用文件扩展名来键入同样的行为? Do you need to read the first few bytes of a file to determine its MIME type as well? 您是否需要读取文件的前几个字节以确定其MIME类型?


#4楼

For ASP.NET or other 对于ASP.NET或其他

The options were changed a bit in ASP.NET Core, here they are ( credits ): 这些选项在ASP.NET Core中有所改变,在这里它们是( 学分 ):

  • new FileExtensionContentTypeProvider().TryGetContentType(fileName, out contentType); (vNext only) (仅限vNext)

    • Never tested, but looks like you can officially expand the mime types list via the exposed Mappings property. 从未测试过,但看起来你可以通过暴露的Mappings属性正式扩展mime类型列表。
  • Use the MimeTypes NuGet package 使用MimeTypes NuGet包
  • Copy the MimeMappings file from the reference source of the .NET Framework 从.NET Framework的引用源复制MimeMappings文件

For .NET Framework >= 4.5: 对于.NET Framework> = 4.5:

Use the System.Web.MimeMapping.GetMimeMapping method, that is part of the BCL in .NET Framework 4.5: 使用System.Web.MimeMapping.GetMimeMapping方法,该方法是.NET Framework 4.5中BCL的一部分:

string mimeType = MimeMapping.GetMimeMapping(fileName);

If you need to add custom mappings you probably can use reflection to add mappings to the BCL MimeMapping class, it uses a custom dictionary that exposes this method, so you should invoke the following to add mappings (never tested tho, but should prob. work). 如果你需要添加自定义映射,你可以使用反射来为BCL MimeMapping类添加映射,它使用一个公开此方法的自定义字典,所以你应该调用以下内容来添加映射(从未测试过,但应该是prob。 )。

Anyway, when using reflection to add MIME types, be aware that since you're accessing a private field, its name might change or even be totally removed, so you should be extra cautious and add double checks and provide fail safe action for every step. 无论如何,当使用反射添加MIME类型时,请注意,因为您正在访问私有字段,其名称可能会更改甚至完全删除,因此您应该格外谨慎并添加双重检查并为每个步骤提供故障安全操作。

MimeMapping._mappingDictionary.AddMapping(string fileExtension, string mimeType)

#5楼

FileExtension handle the file extension and not the Mime. FileExtension处理文件扩展名而不是Mime。 The user can change the file extension, so check the Mime. 用户可以更改文件扩展名,因此请检查Mime。 The codes examples associate Mime by file extension, this is wrong and does not work. 代码示例通过文件扩展名关联Mime,这是错误的,不起作用。

Need to get the contenttype file and check if the table Mime contetType this file according to the file extension. 需要获取contenttype文件并检查表Mime contetType根据文件扩展名contetType此文件。 Now, how to get the ContentType of the file? 现在,如何获取文件的ContentType

Using FileUpload is thus: FileUpload.PostedFile.ContentType; 因此使用FileUpload: FileUpload.PostedFile.ContentType; Now if I already have the file, as caught your ContentType? 现在,如果我已经拥有该文件,那么抓住了您的ContentType?


#6楼

Inspired by Samuel's answer, I wrote an improved version: 受Samuel的回答启发,我写了一个改进的版本:

  • Also works when extension is uppercase. 扩展名为大写时也可以。
  • Take filename as input, handle files without extensions gracefully. 将文件名作为输入,正常处理没有扩展名的文件。
  • Don't include "." 不要包含“。” in keys. 在钥匙。
  • List from Apache , for which I wrote a small transformation script . 来自Apache的列表,我为此编写了一个小的转换脚本 。

The resulting source code is over 30K characters so I can't post it here, check it on Github . 结果源代码超过30K字符,所以我不能在这里发布, 在Github上检查它。

从文件扩展名获取MIME类型相关推荐

  1. java7 javascript引擎_Java7中脚本引擎的一般用法,共三种方法获得JavaScript引擎:名称、文件扩展名、MIME类型 | 学步园...

    package com.sino.java7; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; i ...

  2. 根据文件名或文件扩展名获取文件的默认图标

    新建一个vs2010 窗体项目,新建按钮button和图片picturebox 下面是程序 using System; using System.Collections.Generic; using ...

  3. 根据文件扩展名获取系统图标

    1 /// <summary> 2 /// 根据文件后缀名获取系统图标. 3 /// </summary> 4 /// <param name="extensi ...

  4. 如何让IIS支持未知文件扩展名下载(转)

    下面让IIS6支持caj文件格式下载: 要为特定扩展名定义 MIME 类型,请按照下列步骤操作: 1. 打开 IIS Microsoft 管理控制台 (MMC),右键单击本地计算机名称,然后单击&qu ...

  5. php7 获取文件类型,太简单了!PHP获取文件扩展名的7中方法

    PHP中获取文件扩展名的方法 第一种:$file = 'x.y.z.png'; echo substr(strrchr($file, '.'), 1); 解析:strrchr($file, '.') ...

  6. c++如何获取文件时间_3分钟短文 | PHP 如何优雅地获取文件扩展名?别再explode了

    引言 通常文件扩展名在Linux系统上是没有意义的,仅是用来标记该文件属于哪种类型?但是大多数,我们从浏览器上访问Web程序.提交给服务器的文件是带有扩展名的,比如,图片上传,文件上传就是比较经典的一 ...

  7. php 获取文件扩展名的方法,PHP获取文件扩展名的4种方法,_PHP教程

    PHP获取文件扩展名的4种方法, 本文实例讲述了PHP获取文件扩展名的4种方法.分享给大家供大家参考,具体如下: $filename="123.jpg"; //方法一: funct ...

  8. java 获取文件扩展名_如何在Java中获取文件扩展名

    java 获取文件扩展名 Sometimes while working with files, we need to process them differently based on their ...

  9. 知道文件扩展名,如何获得在系统中该文件类型的图标

    -----------------------   //  FAQ整理:ccrun(老妖)   //  2005.11.30   ----------------------- 请教:知道文件扩展名, ...

最新文章

  1. R语言ggplot2可视化散点图、可视化两个数值变量之间的关系、使用geom_smooth函数基于loess方法拟合数据点之间的趋势关系曲线、自定义数据点的大小、色彩、添加主标题、副标题、题注信息
  2. 怎么修改云服务器项目路径,云服务器tomcat的项目路径怎么设置
  3. 接口到底是什么(一语道破)
  4. ajax异步查询demo,ASP.NET中AJAX的异步加载(Demo演示)
  5. [Python]小甲鱼Python视频第002课(第一个游戏)课后题及参考解答
  6. Memetic Algorithm(文化基因算法)
  7. android 禁用dlsym_Android7.0对dlopen的改变——读取私有.so结果变化
  8. USACO SEC.1.3 No.1 Mixing Milk
  9. linux删除第二列为空_Linux之基本文本处理工具(二)
  10. python 模拟用户点击浏览器_使用python进行模拟浏览器操作
  11. 位移运算符 ( ShiftOperator )
  12. java json删除指定元素_简洁而优雅,Python Tablib实现将数据导出为Excel, Json等N种格式...
  13. 敏捷软件开发VS传统软件开发
  14. php命令模式,老生常谈PHP面向对象之命令模式(必看篇)
  15. fortran95 文件读写等操作的字段参数总结
  16. 经典Hive-SQL面试题及答案
  17. 佛大计算机控制系统期末试卷,[设计]统考计算机模拟练习试题及答案基础知识...
  18. 使用JavaVisualVM远程监控JVM虚拟机
  19. Kinect 姿势检测
  20. 浅析游戏音效制作后期的创新

热门文章

  1. ABBYY PDF Transformer+功能概述
  2. 第十一篇 浅拷贝和深拷贝
  3. 洛谷1196 银河英雄传说(并查集)
  4. 《机器学习实战》学习笔记第七章 —— AdaBoost元算法
  5. Java工程师成神之路思维导图
  6. JsonHelper(Json帮助类)
  7. 设计模式(七): 通过转接头来观察适配器模式(Adapter Pattern)
  8. Swift学习笔记 In-out形参
  9. git 使用http方式的一个小问题
  10. Windows Phone开发(1):概论