<p>How do people usually detect the MIME type of an uploaded file using ASP.NET?</p>

[{'answer_id': 81312, 'author': 'Kinjal Dixit', 'author_id': 6629, 'author_profile': 'https://Stackoverflow.com/users/6629', 'pm_score': 6, 'selected': True, 'text': '<p>in the aspx page:</p>\n\n<pre><code>&lt;asp:FileUpload ID="FileUpload1" runat="server" /&gt;\n</code></pre>\n\n<p>in the codebehind (c#):</p>\n\n<pre><code>string contentType = FileUpload1.PostedFile.ContentType\n</code></pre>\n'}, {'answer_id': 1238450, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 4, 'selected': False, 'text': '<p>The above code will not give correct content type if file is renamed and uploaded.</p>\n\n<p>Please use this code for that</p>\n\n<pre><code>using System.Runtime.InteropServices;\n\n[DllImport("urlmon.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = false)]\nstatic extern int FindMimeFromData(IntPtr pBC,\n    [MarshalAs(UnmanagedType.LPWStr)] string pwzUrl,\n    [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.I1, SizeParamIndex = 3)] byte[] pBuffer,\n    int cbSize,\n    [MarshalAs(UnmanagedType.LPWStr)] string pwzMimeProposed,\n    int dwMimeFlags, out IntPtr ppwzMimeOut, int dwReserved);\n\npublic static string getMimeFromFile(HttpPostedFile file)\n{\n    IntPtr mimeout;\n\n    int MaxContent = (int)file.ContentLength;\n    if (MaxContent &gt; 4096) MaxContent = 4096;\n\n    byte[] buf = new byte[MaxContent];\n    file.InputStream.Read(buf, 0, MaxContent);\n    int result = FindMimeFromData(IntPtr.Zero, file.FileName, buf, MaxContent, null, 0, out mimeout, 0);\n\n    if (result != 0)\n    {\n        Marshal.FreeCoTaskMem(mimeout);\n        return "";\n    }\n\n    string mime = Marshal.PtrToStringUni(mimeout);\n    Marshal.FreeCoTaskMem(mimeout);\n\n    return mime.ToLower();\n}\n</code></pre>\n'}, {'answer_id': 1814291, 'author': 'Richard Szalay', 'author_id': 3603, 'author_profile': 'https://Stackoverflow.com/users/3603', 'pm_score': 3, 'selected': False, 'text': '<p>While aneesh is correct in saying that the content type of the HTTP request may not be correct, I don\'t think that the marshalling for the unmanaged call is worth it. If you need to fall back to extension-to-mimetype mappings, just "borrow" the code from System.Web.MimeMapping.cctor (use Reflector). This dictionary approach is more than sufficient and doesn\'t require the native call.</p>\n'}, {'answer_id': 72055878, 'author': 'Morten Brudvik', 'author_id': 847570, 'author_profile': 'https://Stackoverflow.com/users/847570', 'pm_score': 0, 'selected': False, 'text': '<p>Get MIME type from a file in ASP.NET Core</p>\n<pre><code>public string GetMimeType(string filePath)\n{\n    var provider = new FileExtensionContentTypeProvider();\n\n    if (!provider.TryGetContentType(filePath, out var contentType))\n        contentType = &quot;application/octet-stream&quot;; // fallback: unknown binary type\n        \n    return contentType;\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/81283', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/15396/']