是否存在基于文件扩展名返回内容类型的内置函数?
答案 0 :(得分:16)
不是我知道的。但是你可以使用这段代码:
using Microsoft.Win32;
RegistryKey key = Registry.ClassesRoot.OpenSubKey(extension);
string contentType = key.GetValue("Content Type").ToString();
您需要为错误处理添加额外的代码。
注意:扩展名需要以点为前缀,例如.txt
。
答案 1 :(得分:6)
FYKI,检查\ HKEY_CLASSES_ROOT \ MIME \ Database \ Content Type下的注册表。将有内容类型和文件扩展名列表。如果您可以通过Windows API加载此信息,那么您可以获得内容类型映射的文件扩展名。
HTH
更新:[来源] [1]
public string GetMIMEType(string filepath)
{
FileInfo fileInfo = new FileInfo(filepath);
string fileExtension = fileInfo.Extension.ToLower();
// direct mapping which is fast and ensures these extensions are found
switch (fileExtension)
{
case "htm":
case "html":
return "text/html";
case "js":
return "text/javascript"; // registry may return "application/x-javascript"
}
// see if we can find extension info anywhere in the registry
//Note : there is not a ContentType key under ALL the file types , check Run --> regedit , then extensions !!!
RegistryPermission regPerm = new RegistryPermission(RegistryPermissionAccess.Read, @"\\HKEY_CLASSES_ROOT");
// looks for extension with a content type
RegistryKey rkContentTypes = Registry.ClassesRoot.OpenSubKey(fileExtension);
if (rkContentTypes != null)
{
object key = rkContentTypes.GetValue("Content Type");
if (key != null)
return key.ToString().ToLower();
}
// looks for a content type with extension
// Note : This would be problem if multiple extensions associate with one content type.
RegistryKey typeKey = Registry.ClassesRoot.OpenSubKey(@"MIME\Database\Content Type");
foreach (string keyname in typeKey.GetSubKeyNames())
{
RegistryKey curKey = typeKey.OpenSubKey(keyname);
if (curKey != null)
{
object extension = curKey.GetValue("Extension");
if (extension != null)
{
if (extension.ToString().ToLower() == fileExtension)
{
return keyname;
}
}
}
}
return null;
}
[1]:http://www.codeproject.com/KB/dotnet/ContentType.aspx?msg=2903389#xx2903389xx enter code here
答案 2 :(得分:6)
自.Net Framework 4.5以来,有一个类System.Web.MimeMapping
,它有一个完整的mime类型库,其中包含获取所请求的mime类型的方法。
请参阅:http://msdn.microsoft.com/en-us/library/system.web.mimemapping(v=vs.110).aspx
或实施GetMimeMapping
:https://referencesource.microsoft.com/#System.Web/MimeMapping.cs
答案 3 :(得分:2)
对于或多或少的完整列表,请查看下面的地图(C ++,但它可以直接将其更改为C#)。
这就是Google / Chrome识别内容类型表单文件扩展的方式(不知道如何通过客户端的JavaScript或服务器端的POST来生成电子邮件正文 - 我的猜测是后者)。我使用以下技巧得到了这个列表:
搜索互联网上最常用的文件扩展名。很幸运能找到一个html表格,以便我可以将它复制粘贴到Excel中,以获得一个只有扩展名的清单。
创建了一个包含少量内容字符的小文件 test 。
使用(2)中的扩展列表和一些命令行魔术来创建文件 test.TXT,test.HTM,test.TIFF 等。 Linux变体是for file in test.TXT test.HTM test.TIFF ... ; do cp test "$file"; done;
将所有这些文件作为附件发送到我的Gmail。请注意,出于安全原因,某些文件(如.exe和.zip)已被过滤掉。 以下代码中缺少这些内容!
在gmail中,下载了原文,可以看到每个附加文件的Content-Type: application/pdf; name="example.pdf"
行。在VIM中解析了以下列表。
如果要添加下面列表中没有的扩展名 - 创建小文件 sample.xyz ,请将其发送给自己并查看Content-Type的原始电子邮件。这就是诀窍。希望它有所帮助!
P.S。我知道这不是最好的列表,但我已经足够了解如何以编程方式发送电子邮件附件并选择Content-Type by file extension和gmail一样好。如果某些扩展类型“缺失”,请不要苛刻。
static const map<string, string> ContentTypes = {
{ "TXT","text/plain" },
{ "HTM","text/html" },
{ "TIFF","image/tiff" },
{ "TMP","application/octet-stream" },
{ "TOAST","application/octet-stream" },
{ "TORRENT","application/x-bittorrent" },
{ "TTF","application/x-font-ttf" },
{ "UUE","application/octet-stream" },
{ "VCD","application/x-cdlink" },
{ "VCF","text/x-vcard" },
{ "VCXPROJ","application/xml" },
{ "VOB","application/octet-stream" },
{ "WAV","audio/x-wav" },
{ "WMA","audio/x-ms-wma" },
{ "WMV","video/x-ms-wmv" },
{ "WPD","application/wordperfect" },
{ "WPS","application/octet-stream" },
{ "XCODEPROJ","application/octet-stream" },
{ "XHTML","application/xhtml+xml" },
{ "XLR","application/octet-stream" },
{ "XLS","application/vnd.ms-excel" },
{ "XLSX","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" },
{ "XML","text/xml" },
{ "YUV","application/octet-stream" },
{ "ZIPX","application/octet-stream" },
{ "3DM","application/octet-stream" },
{ "3DS","application/octet-stream" },
{ "3G2","video/3gpp2" },
{ "3GP","video/3gpp" },
{ "ACCDB","application/octet-stream" },
{ "AI","application/illustrator" },
{ "AIF","audio/x-aiff" },
{ "APK","application/vnd.android.package-archive" },
{ "APP","application/octet-stream" },
{ "ASF","video/x-ms-asf" },
{ "ASP","application/octet-stream" },
{ "ASPX","application/xml" },
{ "AVI","video/x-msvideo" },
{ "BAK","application/octet-stream" },
{ "BIN","application/octet-stream" },
{ "BMP","image/bmp" },
{ "C","text/x-csrc" },
{ "CAB","application/octet-stream" },
{ "CBR","application/octet-stream" },
{ "CER","application/x-x509-ca-cert" },
{ "CFG","application/octet-stream" },
{ "CFM","application/octet-stream" },
{ "CGI","application/octet-stream" },
{ "CLASS","application/octet-stream" },
{ "CPP","text/x-c++src" },
{ "CRDOWNLOAD","application/octet-stream" },
{ "CRX","application/x-chrome-extension" },
{ "CS","text/plain" },
{ "CSR","application/octet-stream" },
{ "CSS","text/css" },
{ "CSV","text/csv" },
{ "CUE","application/octet-stream" },
{ "CUR","application/octet-stream" },
{ "DAT","application/octet-stream" },
{ "DB","application/octet-stream" },
{ "DBF","application/octet-stream" },
{ "DDS","image/vnd.ms-dds" },
{ "DEB","application/x-debian-package" },
{ "DEM","application/octet-stream" },
{ "DESKTHEMEPACK","application/octet-stream" },
{ "DLL","application/octet-stream" },
{ "DMG","application/octet-stream" },
{ "DMP","application/octet-stream" },
{ "DOC","application/msword" },
{ "DOCX","application/vnd.openxmlformats-officedocument.wordprocessingml.document" },
{ "DRV","application/octet-stream" },
{ "DTD","application/xml-dtd" },
{ "DWG","application/octet-stream" },
{ "DXF","application/dxf" },
{ "EPS","application/postscript" },
{ "FLA","application/octet-stream" },
{ "FLV","video/x-flv" },
{ "FNT","application/octet-stream+fnt" },
{ "FON","application/octet-stream+fon" },
{ "GADGET","application/octet-stream" },
{ "GAM","application/octet-stream" },
{ "GED","application/octet-stream" },
{ "GIF","image/gif" },
{ "GPX","application/gpx+xml" },
{ "GZ","application/x-gzip" },
{ "H","text/x-chdr" },
{ "HQX","application/mac-binhex40" },
{ "HTML","text/html" },
{ "ICNS","application/octet-stream" },
{ "ICO","image/x-icon" },
{ "ICS","text/calendar" },
{ "IFF","application/octet-stream" },
{ "INDD","application/octet-stream" },
{ "INI","application/octet-stream" },
{ "ISO","application/octet-stream" },
{ "JAVA","application/octet-stream" },
{ "JPG","image/jpeg" },
{ "JSP","application/octet-stream" },
{ "KEY","application/octet-stream" },
{ "KEYCHAIN","application/octet-stream" },
{ "KML","application/vnd.google-earth.kml+xml" },
{ "KMZ","application/vnd.google-earth.kmz" },
{ "LOG","application/octet-stream" },
{ "LUA","application/octet-stream" },
{ "M","application/octet-stream" },
{ "M3U","audio/x-mpegurl" },
{ "M4A","audio/mp4" },
{ "M4V","video/x-m4v" },
{ "MAX","application/octet-stream" },
{ "MDB","application/octet-stream" },
{ "MDF","application/octet-stream" },
{ "MID","audio/midi" },
{ "MIM","application/octet-stream" },
{ "MOV","video/quicktime" },
{ "MP3","audio/mpeg" },
{ "MP4","video/mp4" },
{ "MPA","audio/mpeg" },
{ "MPG","video/mpeg" },
{ "MSG","application/octet-stream" },
{ "NES","application/octet-stream" },
{ "OBJ","application/octet-stream" },
{ "ODT","application/vnd.oasis.opendocument.text" },
{ "OTF","application/vnd.oasis.opendocument.formula-template" },
{ "PAGES","application/x-iwork-pages-sffpages" },
{ "PART","application/octet-stream" },
{ "PCT","application/octet-stream" },
{ "PDB","chemical/x-pdb" },
{ "PDF","application/pdf" },
{ "PHP","application/x-httpd-php" },
{ "PKG","application/octet-stream" },
{ "PL","application/octet-stream" },
{ "PLUGIN","application/octet-stream" },
{ "PNG","image/png" },
{ "PPS","application/vnd.ms-powerpoint" },
{ "PPT","application/vnd.ms-powerpoint" },
{ "PPTX","application/vnd.openxmlformats-officedocument.presentationml.presentation" },
{ "PRF","application/pics-rules" },
{ "PS","application/postscript" },
{ "PSD","application/photoshop" },
{ "PSPIMAGE","application/octet-stream" },
{ "PY","application/octet-stream" },
{ "RM","audio/x-pn-realaudio" },
{ "ROM","application/octet-stream" },
{ "RPM","application/x-rpm" },
{ "RSS","application/octet-stream" },
{ "RTF","application/rtf" },
{ "SAV","application/octet-stream" },
{ "SDF","application/octet-stream" },
{ "SH","application/x-sh" },
{ "SITX","application/octet-stream" },
{ "SLN","text/plain" },
{ "SQL","application/octet-stream" },
{ "SRT","application/octet-stream" },
{ "SVG","image/svg+xml" },
{ "SWF","application/x-shockwave-flash" },
{ "SWIFT","application/octet-stream" },
{ "TAX2016","application/octet-stream" },
{ "TEX","application/x-tex" }
};
答案 4 :(得分:1)
如果是上传的文件。 您可以将contenttype存储在数据库的一列中,以便以后在上载文件时使用。 HttpPostedFile类有一个名为ContentType的属性。
答案 5 :(得分:0)
这是我写的:
public string GetContentTypeByExtension(string strExtension)
{
switch (strExtension)
{
case ".fif":
return "application/fractals";
break;
case ".hta":
return "application/hta";
break;
case ".hqx":
return "application/mac-binhex40";
break;
case ".vsi":
return "application/ms-vsi";
break;
case ".p10":
return "application/pkcs10";
break;
case ".p7m":
return "application/pkcs7-mime";
break;
case ".p7s":
return "application/pkcs7-signature";
break;
case ".cer":
return "application/pkix-cert";
break;
case ".crl":
return "application/pkix-crl";
break;
case ".ps":
return "application/postscript";
break;
case ".setpay":
return "application/set-payment-initiation";
break;
case ".setreg":
return "application/set-registration-initiation";
break;
case ".sst":
return "application/vnd.ms-pki.certstore";
break;
case ".pko":
return "application/vnd.ms-pki.pko";
break;
case ".cat":
return "application/vnd.ms-pki.seccat";
break;
case ".stl":
return "application/vnd.ms-pki.stl";
break;
case ".wpl":
return "application/vnd.ms-wpl";
break;
case ".xps":
return "application/vnd.ms-xpsdocument";
break;
case ".z":
return "application/x-compress";
break;
case ".tgz":
return "application/x-compressed";
break;
case ".gz":
return "application/x-gzip";
break;
case ".ins":
return "application/x-internet-signup";
break;
case ".iii":
return "application/x-iphone";
break;
case ".jtx":
return "application/x-jtx+xps";
break;
case ".latex":
return "application/x-latex";
break;
case ".nix":
return "application/x-mix-transfer";
break;
case ".asx":
return "application/x-mplayer2";
break;
case ".application":
return "application/x-ms-application";
break;
case ".wmd":
return "application/x-ms-wmd";
break;
case ".wmz":
return "application/x-ms-wmz";
break;
case ".xbap":
return "application/x-ms-xbap";
break;
case ".p12":
return "application/x-pkcs12";
break;
case ".p7b":
return "application/x-pkcs7-certificates";
break;
case ".p7r":
return "application/x-pkcs7-certreqresp";
break;
case ".sit":
return "application/x-stuffit";
break;
case ".tar":
return "application/x-tar";
break;
case ".man":
return "application/x-troff-man";
break;
case ".cer":
return "application/x-x509-ca-cert";
break;
case ".zip":
return "application/x-zip-compressed";
break;
case ".xaml":
return "application/xaml+xml";
break;
case ".xml":
return "application/xml";
break;
case ".aiff":
return "audio/aiff";
break;
case ".au":
return "audio/basic";
break;
case ".mid":
return "audio/mid";
break;
case ".mid":
return "audio/midi";
break;
case ".mp3":
return "audio/mp3";
break;
case ".mp3":
return "audio/mpeg";
break;
case ".m3u":
return "audio/mpegurl";
break;
case ".mp3":
return "audio/mpg";
break;
case ".wav":
return "audio/wav";
break;
case ".aiff":
return "audio/x-aiff";
break;
case ".mid":
return "audio/x-mid";
break;
case ".mid":
return "audio/x-midi";
break;
case ".mp3":
return "audio/x-mp3";
break;
case ".mp3":
return "audio/x-mpeg";
break;
case ".m3u":
return "audio/x-mpegurl";
break;
case ".mp3":
return "audio/x-mpg";
break;
case ".wax":
return "audio/x-ms-wax";
break;
case ".wma":
return "audio/x-ms-wma";
break;
case ".wav":
return "audio/x-wav";
break;
case ".bmp":
return "image/bmp";
break;
case ".gif":
return "image/gif";
break;
case ".jpg":
return "image/jpeg";
break;
case ".jpg":
return "image/pjpeg";
break;
case ".png":
return "image/png";
break;
case ".tiff":
return "image/tiff";
break;
case ".ico":
return "image/x-icon";
break;
case ".png":
return "image/x-png";
break;
case ".mid":
return "midi/mid";
break;
case ".dwfx":
return "model/vnd.dwfx+xps";
break;
case ".css":
return "text/css";
break;
case ".323":
return "text/h323";
break;
case ".htm":
return "text/html";
break;
case ".uls":
return "text/iuls";
break;
case ".txt":
return "text/plain";
break;
case ".wsc":
return "text/scriptlet";
break;
case ".htt":
return "text/webviewhtml";
break;
case ".htc":
return "text/x-component";
break;
case ".vcf":
return "text/x-vcard";
break;
case ".xml":
return "text/xml";
break;
case ".avi":
return "video/avi";
break;
case ".mpeg":
return "video/mpeg";
break;
case ".mpeg":
return "video/mpg";
break;
case ".avi":
return "video/msvideo";
break;
case ".mpeg":
return "video/x-mpeg";
break;
case ".mpeg":
return "video/x-mpeg2a";
break;
case ".asx":
return "video/x-ms-asf";
break;
case ".asx":
return "video/x-ms-asf-plugin";
break;
case ".wm":
return "video/x-ms-wm";
break;
case ".wmv":
return "video/x-ms-wmv";
break;
case ".wmx":
return "video/x-ms-wmx";
break;
case ".wvx":
return "video/x-ms-wvx";
break;
case ".avi":
return "video/x-msvideo";
break;
}
}