内容类型扩展名

问题描述 投票:16回答:6

是否有任何基于文件扩展名返回内容类型的内置函数?

c# content-type
6个回答
16
投票

从来没听说过。但是你可以使用这段代码:

using Microsoft.Win32;

RegistryKey key = Registry.ClassesRoot.OpenSubKey(extension);
string contentType = key.GetValue("Content Type").ToString();

您需要添加额外的代码以进行错误处理。

注意:扩展名需要以点为前缀,例如.txt


6
投票

FYKI,检查\ HKEY_CLASSES_ROOT \ MIME \ Database \ Content Type下的注册表。将有内容类型和文件扩展名列表。如果您可以通过Windows API加载此信息,那么您可以获得内容类型映射的文件扩展名。

心连心

更新:[来源] [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#xx2903389xxenter code here


6
投票

从.Net Framework 4.5开始,有一个类System.Web.MimeMapping,它有一个完整的mime类型库,其中包含获取所请求的mime类型的方法。

见:http://msdn.microsoft.com/en-us/library/system.web.mimemapping(v=vs.110).aspx

或者为了实施GetMimeMappinghttps://referencesource.microsoft.com/#System.Web/MimeMapping.cs


2
投票

有关或多或少的完整列表,请查看下面的地图(C ++,但它可以直接将其更改为C#)。

这就是Google / Chrome识别内容类型表单文件扩展的方式(不知道如何通过客户端的JavaScript或服务器端的POST生成电子邮件正文 - 我的猜测是后者)。我使用以下技巧获得此列表:

  1. 搜索了互联网上最常用的文件扩展名。很幸运能找到一个html表,以便我可以将它复制粘贴到Excel中,以获得一个干净的扩展列表。
  2. 创建了一个包含少量内容字符的小文件测试。
  3. 使用(2)中的扩展列表和一些命令行魔术来创建文件test.TXT,test.HTM,test.TIFF等。 Linux变种是qazxsw poi
  4. 将所有这些文件作为附件发送到我的Gmail。请注意,出于安全原因,某些文件(如.exe和.zip)已被过滤掉。它们在下面的代码中丢失了!
  5. 在gmail中,下载了原始文件,可以看到每个附加文件的for file in test.TXT test.HTM test.TIFF ... ; do cp test "$file"; done;行。解析在VIM中获取下面的列表。

如果要添加下面列表中没有的扩展名 - 创建小文件sample.xyz,请将其发送给自己并查看Content-Type的原始电子邮件。这就是诀窍。希望能帮助到你!

附:我知道这不是最好的列表,但我已经足够了解如何以编程方式发送电子邮件附件并选择Content-Type by file extension和gmail一样好。如果某些扩展类型“缺失”,请不要苛刻。

Content-Type: application/pdf; name="example.pdf"

1
投票

如果是上传的文件。您可以将contenttype存储在数据库的一列中,以便以后在上载文件时使用。 HttpPostedFile类有一个名为ContentType的属性。


0
投票

这是我写的一个:

    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" }
    };
© www.soinside.com 2019 - 2024. All rights reserved.