NetCore MimeMapping获取MIME

本文介绍了如何在.NET Core项目中利用C#代码获取文件的MIME类型,参考自https://www.cnblogs.com/huanyun/p/11213208.html。
摘要由CSDN通过智能技术生成
public static class MimeMapping
    {
        private abstract class MimeMappingDictionaryBase
        {
            private readonly Dictionary<string, string> _mappings = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

            private static readonly char[] _pathSeparatorChars = new char[3]
            {
                Path.DirectorySeparatorChar,
                Path.AltDirectorySeparatorChar,
                Path.VolumeSeparatorChar
            };

            private bool _isInitialized;

            protected void AddMapping(string fileExtension, string mimeType)
            {
                _mappings.Add(fileExtension, mimeType);
            }

            private void AddWildcardIfNotPresent()
            {
                if (!_mappings.ContainsKey(".*"))
                {
                    AddMapping(".*", "application/octet-stream");
                }
            }

            private void EnsureMapping()
            {
                if (!_isInitialized)
                {
                    lock (this)
                    {
                        if (!_isInitialized)
                        {
                            PopulateMappings();
                            AddWildcardIfNotPresent();
                            _isInitialized = true;
                        }
                    }
                }
            }

            protected abstract void PopulateMappings();

            private static string GetFileName(string path)
            {
                int num = path.LastIndexOfAny(_pathSeparatorChars);
                if (num < 0)
                {
                    return path;
                }
                return path.Substring(num);
            }

            public string GetMimeMapping(string fileName)
            {
                EnsureMapping();
                fileName = GetFileName(fileName);
                for (int i = 0; i < fileName.Length; i++)
                {
                    if (fileName[i] == '.' && _mappings.TryGetValue(fileName.Substring(i), out string result))
                    {
                        return result;
                    }
                }
                return _mappings[".*"];
            }
        }

        private sealed class MimeMappingDictionaryClassic : MimeMappingDictionaryBase
        {
            protected override void PopulateMappings()
            {
                base.AddMapping(".323", "text/h323");
                base.AddMapping(".aaf", "application/octet-stream");
                base.AddMapping(".aca", "application/octet-stream");
                base.AddMapping(".accdb", "application/msaccess");
                base.AddMapping(".accde", "application/msaccess");
                base.AddMapping(".accdt", "application/msaccess");
                base.AddMapping(".acx", "application/internet-property-stream");
                base.AddMapping(".afm", "application/octet-stream");
                base.AddMapping(".ai", "application/postscript");
                base.AddMapping(".aif", "audio/x-aiff");
                base.AddMapping(".aifc", "audio/aiff");
                base.AddMapping(".aiff", "audio/aiff");
                base.AddMapping(".application", "application/x-ms-application");
                base.AddMapping(".art", "image/x-jg");
                base.AddMapping(".asd", "application/octet-stream");
                base.AddMapping(".asf", "video/x-ms-asf");
                base.AddMapping(".asi", "application/octet-stream");
                base.AddMapping(".asm", "text/plain");
                base.AddMapping(".asr", "video/x-ms-asf");
                base.AddMapping(".asx", "video/x-ms-asf");
                base.AddMapping(".atom", "application/atom+xml");
                base.AddMapping(".au", "audio/basic");
                base.AddMapping(".avi", "video/x-msvideo");
                base.AddMapping(".axs", "application/olescript");
                base.AddMapping(".bas", "text/plain");
                base.AddMapping(".bcpio", "application/x-bcpio");
                base.AddMapping(".bin", "application/octet-stream");
                base.AddMapping(".bmp", "image/bmp");
                base.AddMapping(".c", "text/plain");
                base.AddMapping(".cab", "application/octet-stream");
                base.AddMapping(".calx", "application/vnd.ms-office.calx");
                base.AddMapping(".cat", "application/vnd.ms-pki.seccat");
                base.AddMapping(".cdf", "application/x-cdf");
                base.AddMapping(".chm", "application/octet-stream");
                base.AddMapping(".class", "application/x-java-applet");
                base.AddMapping(".clp", "application/x-msclip");
                base.AddMapping(".cmx", "image/x-cmx");
                base.AddMapping(".cnf", "text/plain");
                base.AddMapping(".cod", "image/cis-cod");
                base.AddMapping(".cpio", "application/x-cpio");
                base.AddMapping(".cpp", "text/plain");
                base.AddMapping(".crd", "application/x-mscardfile");
                base.AddMapping(".crl", "application/pkix-crl");
                base.AddMapping(".crt", "application/x-x509-ca-cert");
                base.AddMapping(".csh", "application/x-csh");
                base.AddMapping(".css", "text/css");
                base.AddMapping(".csv", "application/octet-stream");
                base.AddMapping(".cur", "application/octet-stream");
                base.AddMapping(".dcr", "application/x-director");
                base.AddMapping(".deploy", "application/octet-stream");
                base.AddMapping(".der", "application/x-x509-ca-cert");
                base.AddMapping(".dib", "image/bmp");
                base.AddMapping(".dir", "application/x-director");
                base.AddMapping(".disco", "text/xml");
                base.AddMapping(".dll", "application/x-msdownload");
                base.AddMapping(".dll.config", "text/xml"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值