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"
NetCore MimeMapping获取MIME
最新推荐文章于 2024-07-15 18:00:00 发布
本文介绍了如何在.NET Core项目中利用C#代码获取文件的MIME类型,参考自https://www.cnblogs.com/huanyun/p/11213208.html。
摘要由CSDN通过智能技术生成