[ASP.NET] 拡張子から MIEM TYPE を取得する方法
任意の形式のファイルをDLするとき、拡張子が分からないとMIMETYPEを正しく指定できません。拡張子からMIMETYPEを引っ張てこれる機能が欲しかったので探しました。
.NET 4.5以降では、MimeMapping.GetMimeMapping メソッド が用意されています。
System.Web配下のこのメソッドは、指定したファイル名の MIME マッピングを返します。
使用例
// application/pdf
string mimetype = System.Web.MimeMapping.GetMimeMapping("sample.pdf");
これでファイル名(拡張子)からMIMETYPEを取得することができました。
[.NET] 拡張子と対応するMIMETYPEを列挙
レジストリに登録されている内容から拡張子とMIMETYPEの組み合わせを列挙することができます。おそらく上のマッピングもこの対応に基づいています。
// レジストリに登録されている拡張子とMIMETYPEの組み合わせを列挙
foreach (var key in Microsoft.Win32.Registry.ClassesRoot.GetSubKeyNames())
{
var type = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(key).GetValue("Content Type");
if (type != null)
{
// 拡張子, MIMETYPE
Console.WriteLine($"{key}, {type}");
}
}
とりあえず上のコードで列挙される内容をいかに載せておきます。長いので見なくてもよいです。以上。
| 拡張子 | MIMETYPE |
|---|---|
| .3g2 | video/3gpp2 |
| .3gp | video/3gpp |
| .3gp2 | video/3gpp2 |
| .3gpp | video/3gpp |
| .aac | audio/vnd.dlna.adts |
| .ac3 | audio/vnd.dolby.dd-raw |
| .accountpicture-ms | application/windows-accountpicture |
| .acrobatsecuritysettings | application/vnd.adobe.acrobat-security-settings |
| .AddIn | text/xml |
| .adt | audio/vnd.dlna.adts |
| .adts | audio/vnd.dlna.adts |
| .ai | application/postscript |
| .AIF | audio/aiff |
| .AIFC | audio/aiff |
| .AIFF | audio/aiff |
| .androidproj | Application/xml |
| .appcontent-ms | application/windows-appcontent+xml |
| .application | application/x-ms-application |
| .arw | image/ARW |
| .asax | application/xml |
| .ascx | application/xml |
| .asf | video/x-ms-asf |
| .ashx | application/xml |
| .asm | text/plain |
| .asmx | application/xml |
| .aspx | application/xml |
| .ASX | video/x-ms-asf |
| .AU | audio/basic |
| .avi | video/avi |
| .bmp | image/bmp |
| .c | text/plain |
| .cat | application/vnd.ms-pki.seccat |
| .cc | text/plain |
| .cd | text/plain |
| .cer | application/x-x509-ca-cert |
| .cod | text/plain |
| .config | application/xml |
| .contact | text/x-ms-contact |
| .coverage | application/xml |
| .cpp | text/plain |
| .cr2 | image/CR2 |
| .crl | application/pkix-crl |
| .crt | application/x-x509-ca-cert |
| .crw | image/CRW |
| .cs | text/plain |
| .csh | text/plain |
| .cshader | text/plain |
| .csproj | text/plain |
| .css | text/css |
| .csv | application/vnd.ms-excel |
| .cur | text/plain |
| .cxx | text/plain |
| .datasource | application/xml |
| .dds | image/vnd.ms-dds |
| .def | text/plain |
| .der | application/x-x509-ca-cert |
| .dgml | application/xml |
| .dib | image/bmp |
| .dll | application/x-msdownload |
| .dng | image/DNG |
| .doc | application/msword |
| .docm | application/vnd.ms-word.document.macroEnabled.12 |
| .docx | application/vnd.openxmlformats-officedocument.wordprocessingml.document |
| .dot | application/msword |
| .dotm | application/vnd.ms-word.template.macroEnabled.12 |
| .dotx | application/vnd.openxmlformats-officedocument.wordprocessingml.template |
| .dsh | text/plain |
| .dshader | text/plain |
| .dsp | text/plain |
| .dsw | text/plain |
| .dtcp-ip | application/x-dtcp1 |
| .dtd | application/xml-dtd |
| .dtsConfig | text/xml |
| .DVR-MS | video/x-ms-dvr |
| .dwfx | model/vnd.dwfx+xps |
| .easmx | model/vnd.easmx+xps |
| .ec3 | audio/ec3 |
| .edrwx | model/vnd.edrwx+xps |
| .emf | image/x-emf |
| .eprtx | model/vnd.eprtx+xps |
| .eps | application/postscript |
| .erf | image/ERF |
| .exe | application/x-msdownload |
| .fdf | application/vnd.fdf |
| .fif | application/fractals |
| .filters | Application/xml |
| .flac | audio/x-flac |
| .flv | video/x-flv |
| .fs | text/plain |
| .fsi | text/plain |
| .fsscript | text/plain |
| .fsx | text/plain |
| .fx | text/plain |
| .gif | image/gif |
| .group | text/x-ms-group |
| .gsh | text/plain |
| .gshader | text/plain |
| .gz | application/x-gzip |
| .h | text/plain |
| .hh | text/plain |
| .hlsl | text/plain |
| .hlsli | text/plain |
| .hpp | text/plain |
| .hqx | application/mac-binhex40 |
| .hsh | text/plain |
| .hshader | text/plain |
| .hta | application/hta |
| .htc | text/x-component |
| .htm | text/html |
| .html | text/html |
| .hxa | application/xml |
| .hxc | application/xml |
| .hxd | application/octet-stream |
| .hxe | application/xml |
| .hxf | application/xml |
| .hxh | application/octet-stream |
| .hxi | application/octet-stream |
| .hxk | application/xml |
| .hxq | application/octet-stream |
| .hxr | application/octet-stream |
| .hxs | application/octet-stream |
| .hxt | application/xml |
| .hxv | application/xml |
| .hxw | application/octet-stream |
| .hxx | text/plain |
| .i | text/plain |
| .ico | image/x-icon |
| .idl | text/plain |
| .inc | text/plain |
| .inl | text/plain |
| .iqy | text/x-ms-iqy |
| .jfif | image/jpeg |
| .jnlp | application/x-java-jnlp-file |
| .jpe | image/jpeg |
| .jpeg | image/jpeg |
| .jpg | image/jpeg |
| .jtx | application/x-jtx+xps |
| .jxr | image/vnd.ms-photo |
| .kdc | image/KDC |
| .latex | application/x-latex |
| .library-ms | application/windows-library+xml |
| .lpcm | audio/l16 |
| .lst | text/plain |
| .lzh | application/x-lzh-compressed |
| .m | text/plain |
| .M1V | video/mpeg |
| .m2t | video/vnd.dlna.mpeg-tts |
| .m2ts | video/vnd.dlna.mpeg-tts |
| .M2V | video/mpeg |
| .m3u | audio/x-mpegurl |
| .m4a | audio/mp4 |
| .m4v | video/mp4 |
| .mak | text/plain |
| .man | application/x-troff-man |
| .map | text/plain |
| .master | application/xml |
| .mdp | text/plain |
| .mht | message/rfc822 |
| .mhtml | message/rfc822 |
| .MID | audio/mid |
| .MIDI | audio/mid |
| .mk | text/plain |
| .MKA | audio/x-matroska |
| .mkv | video/x-matroska |
| .mod | video/mpeg |
| .mov | video/quicktime |
| .mp2 | audio/mpeg |
| .MP2V | video/mpeg |
| .mp3 | audio/mpeg |
| .mp4 | video/mp4 |
| .mp4v | video/mp4 |
| .mpa | audio/mpeg |
| .mpe | video/mpeg |
| .mpeg | video/mpeg |
| .mpf | application/vnd.ms-mediapackage |
| .mpg | video/mpeg |
| .mpv2 | video/mpeg |
| .mrw | image/MRW |
| .mts | video/vnd.dlna.mpeg-tts |
| .natvis | text/xml |
| .nef | image/NEF |
| .nrw | image/NRW |
| .odc | text/x-ms-odc |
| .odh | text/plain |
| .odl | text/plain |
| .odp | application/vnd.oasis.opendocument.presentation |
| .orderedtest | application/xml |
| .orf | image/ORF |
| .osdx | application/opensearchdescription+xml |
| .p10 | application/pkcs10 |
| .p12 | application/x-pkcs12 |
| .p7b | application/x-pkcs7-certificates |
| .p7c | application/pkcs7-mime |
| .p7m | application/pkcs7-mime |
| .p7r | application/x-pkcs7-certreqresp |
| .p7s | application/pkcs7-signature |
| .pano | application/vnd.ms-pano |
| application/pdf | |
| .pdfxml | application/vnd.adobe.pdfxml |
| .pdx | application/vnd.adobe.pdx |
| .pef | image/PEF |
| .pfx | application/x-pkcs12 |
| .pkgdef | text/plain |
| .pkgundef | text/plain |
| .pko | application/vnd.ms-pki.pko |
| .png | image/png |
| .pot | application/vnd.ms-powerpoint |
| .potm | application/vnd.ms-powerpoint.template.macroEnabled.12 |
| .potx | application/vnd.openxmlformats-officedocument.presentationml.template |
| .ppa | application/vnd.ms-powerpoint |
| .ppam | application/vnd.ms-powerpoint.addin.macroEnabled.12 |
| .pps | application/vnd.ms-powerpoint |
| .ppsm | application/vnd.ms-powerpoint.slideshow.macroEnabled.12 |
| .ppsx | application/vnd.openxmlformats-officedocument.presentationml.slideshow |
| .ppt | application/vnd.ms-powerpoint |
| .pptm | application/vnd.ms-powerpoint.presentation.macroEnabled.12 |
| .pptx | application/vnd.openxmlformats-officedocument.presentationml.presentation |
| .prf | application/pics-rules |
| .ps | application/postscript |
| .psc1 | application/PowerShell |
| .psess | application/xml |
| .psh | text/plain |
| .pshader | text/plain |
| .pwz | application/vnd.ms-powerpoint |
| .raf | image/RAF |
| .rat | application/rat-file |
| .raw | image/RAW |
| .rc | text/plain |
| .rc2 | text/plain |
| .rct | text/plain |
| .rdlc | application/xml |
| .resjson | text/plain |
| .resx | application/xml |
| .rgs | text/plain |
| .RMI | audio/mid |
| .rqy | text/x-ms-rqy |
| .rtf | application/msword |
| .ruleset | application/xml |
| .rw2 | image/RW2 |
| .rwl | image/RWL |
| .s | text/plain |
| .sct | text/scriptlet |
| .searchConnector-ms | application/windows-search-connector+xml |
| .settings | application/xml |
| .shproj | application/xml |
| .shtml | text/html |
| .sit | application/x-stuffit |
| .sitemap | application/xml |
| .skin | application/xml |
| .sldm | application/vnd.ms-powerpoint.slide.macroEnabled.12 |
| .sldx | application/vnd.openxmlformats-officedocument.presentationml.slide |
| .slk | application/vnd.ms-excel |
| .sln | text/plain |
| .SND | audio/basic |
| .snippet | application/xml |
| .sol | text/plain |
| .sor | text/plain |
| .spc | application/x-pkcs7-certificates |
| .spl | application/futuresplash |
| .sr2 | image/SR2 |
| .srf | text/plain |
| .srw | image/SRW |
| .SSISDeploymentManifest | text/xml |
| .sst | application/vnd.ms-pki.certstore |
| .svc | application/xml |
| .svg | image/svg+xml |
| .swf | application/x-shockwave-flash |
| .tar | application/x-tar |
| .testrunconfig | application/xml |
| .testsettings | application/xml |
| .tgz | application/x-compressed |
| .thmx | application/vnd.ms-officetheme |
| .tif | image/tiff |
| .tiff | image/tiff |
| .tlh | text/plain |
| .tli | text/plain |
| .tod | video/mpeg |
| .trx | application/xml |
| .ts | text/plain |
| .tsx | text/plain |
| .tt | text/plain |
| .tts | video/vnd.dlna.mpeg-tts |
| .txt | text/plain |
| .user | text/plain |
| .uvu | video/vnd.dece.mp4 |
| .vb | text/plain |
| .vbproj | text/plain |
| .vcf | text/x-vcard |
| .vcproj | Application/xml |
| .vcxitems | Application/xml |
| .vcxproj | Application/xml |
| .vdw | application/vnd.ms-visio.viewer |
| .vdx | application/vnd.ms-visio.viewer |
| .vscontent | application/xml |
| .vsct | text/xml |
| .vsd | application/vnd.ms-visio.viewer |
| .vsdm | application/vnd.ms-visio.viewer |
| .vsdx | application/vnd.ms-visio.viewer |
| .vsh | text/plain |
| .vshader | text/plain |
| .vsi | application/ms-vsi |
| .vsix | application/vsix |
| .vsixlangpack | text/xml |
| .vsixmanifest | text/xml |
| .vsmdi | application/xml |
| .vspscc | text/plain |
| .vss | application/vnd.ms-visio.viewer |
| .vsscc | text/plain |
| .vssettings | text/xml |
| .vssm | application/vnd.ms-visio.viewer |
| .vssscc | text/plain |
| .vssx | application/vnd.ms-visio.viewer |
| .vst | application/vnd.ms-visio.viewer |
| .vstemplate | text/xml |
| .vstm | application/vnd.ms-visio.viewer |
| .vsto | application/x-ms-vsto |
| .vstx | application/vnd.ms-visio.viewer |
| .vsx | application/vnd.ms-visio.viewer |
| .vtx | application/vnd.ms-visio.viewer |
| .wav | audio/wav |
| .WAX | audio/x-ms-wax |
| .wbk | application/msword |
| .wdp | image/vnd.ms-photo |
| .website | application/x-mswebsite |
| .wiq | application/xml |
| .wiz | application/msword |
| .wm | video/x-ms-wm |
| .wma | audio/x-ms-wma |
| .WMD | application/x-ms-wmd |
| .wmf | image/x-wmf |
| .wmv | video/x-ms-wmv |
| .WMX | video/x-ms-wmx |
| .WMZ | application/x-ms-wmz |
| .wpl | application/vnd.ms-wpl |
| .wsc | text/scriptlet |
| .wsdl | application/xml |
| .WVX | video/x-ms-wvx |
| .xaml | application/xaml+xml |
| .xbap | application/x-ms-xbap |
| .xdp | application/vnd.adobe.xdp+xml |
| .xdr | application/xml |
| .xfdf | application/vnd.adobe.xfdf |
| .xht | application/xhtml+xml |
| .xhtml | application/xhtml+xml |
| .xla | application/vnd.ms-excel |
| .xlam | application/vnd.ms-excel.addin.macroEnabled.12 |
| .xlk | application/vnd.ms-excel |
| .xll | application/vnd.ms-excel |
| .xlm | application/vnd.ms-excel |
| .xls | application/vnd.ms-excel |
| .xlsb | application/vnd.ms-excel.sheet.binary.macroEnabled.12 |
| .xlsm | application/vnd.ms-excel.sheet.macroEnabled.12 |
| .xlsx | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
| .xlt | application/vnd.ms-excel |
| .xltm | application/vnd.ms-excel.template.macroEnabled.12 |
| .xltx | application/vnd.openxmlformats-officedocument.spreadsheetml.template |
| .xlw | application/vnd.ms-excel |
| .xmap | application/vnd.xmind.xmap |
| .xmind | application/vnd.xmind.workbook |
| .xml | text/xml |
| .xmp | application/vnd.xmind.media-package |
| .xmt | application/vnd.xmind.template |
| .XOML | text/plain |
| .xps | application/vnd.ms-xpsdocument |
| .xrb | application/vnd.xmind.resource-bundle |
| .xrm-ms | text/xml |
| .xsc | application/xml |
| .xsd | application/xml |
| .xsl | text/xml |
| .xslt | application/xml |
| .xss | application/xml |
| .z | application/x-compress |
| .zip | application/x-zip-compressed |
コメントを書く