Option Strict On
Imports System.Runtime.InteropServices
Imports System.IO
Public Class MimeTypes
Private Shared knownTypes As List(Of String)
Private Shared mimeTypes As Dictionary(Of String, String)
<DllImport("urlmon.dll", CharSet:=CharSet.Auto)> _
Private Shared Function FindMimeFromData(ByVal pBC As UInt32, <MarshalAs(UnmanagedType.LPStr)> ByVal pwzUrl As String, <MarshalAs(UnmanagedType.LPArray)> ByVal pBuffer As Byte(), ByVal cbSize As UInt32, <MarshalAs(UnmanagedType.LPStr)> ByVal pwzMimeProposed As String, ByVal dwMimeFlags As UInt32, ByRef ppwzMimeOut As UInt32, ByVal dwReserverd As UInt32) As UInt32
End Function
Public Shared Function GetContentType(ByVal fileName As String) As String
If knownTypes Is Nothing OrElse mimeTypes Is Nothing Then InitializeMimeTypeLists()
Dim contentType As String = ""
Dim extension As String = IO.Path.GetExtension(fileName).Replace(".", "").ToLower
mimeTypes.TryGetValue(extension, contentType)
If String.IsNullOrEmpty(contentType) OrElse knownTypes.Contains(contentType) Then
Dim headerType As String = ScanFileForMimeType(fileName)
If headerType <> "application/octet-stream" OrElse String.IsNullOrEmpty(contentType) Then contentType = headerType
End If
Return contentType
End Function
Private Shared Function ScanFileForMimeType(ByVal fileName As String) As String
Dim buffer As Byte() = New Byte(255) {}
Using fs As New FileStream(fileName, FileMode.Open)
Dim readLength As Integer = CInt(Math.Min(256, fs.Length))
fs.Read(buffer, 0, readLength)
End Using
Try
Dim mimeType As UInt32
FindMimeFromData(0, Nothing, buffer, 256, Nothing, 0, mimeType, 0)
Dim mimeTypePtr As IntPtr = New IntPtr(mimeType)
Dim mime As String = Marshal.PtrToStringUni(mimeTypePtr)
Marshal.FreeCoTaskMem(mimeTypePtr)
If String.IsNullOrEmpty(mime) Then mime = "application/octet-stream"
Return mime
Catch e As Exception
Return "application/octet-stream"
End Try
End Function
Private Shared Sub InitializeMimeTypeLists()
knownTypes = New List(Of String)
knownTypes = New String() {"text/plain", "text/html", "text/xml", "text/richtext", "text/scriptlet", _
"audio/x-aiff", "audio/basic", "audio/mid", "audio/wav", _
"image/gif", "image/jpeg", "image/pjpeg", "image/png", "image/x-png", "image/tiff", "image/bmp", _
"image/x-xbitmap", "image/x-jg", "image/x-emf", "image/x-wmf", "video/avi", "video/mpeg", _
"application/octet-stream", "application/postscript", "application/base64", "application/macbinhex40", _
"application/pdf", "application/xml", "application/atom+xml", "application/rss+xml", _
"application/x-compressed", "application/x-zip-compressed", "application/x-gzip-compressed", _
"application/java", "application/x-msdownload" _
}.ToList
mimeTypes = New Dictionary(Of String, String)
With mimeTypes
.Add("3dm", "x-world/x-3dmf")
.Add("3dmf", "x-world/x-3dmf")
'...
' get the complete list from mendhak's post here:
' http://forums.asp.net/t/1143353.aspx
' or from anywhere else if you can find a more recent updated list.
'...
.Add("zoo", "application/octet-stream")
.Add("zsh", "text/x-script.zsh")
End With
End Sub
End Class