Get the MIME type of a file.


/ Published in: VB.NET
Save to your folder(s)

By running the function GetMimeType(file.name) the following code will return the MIME type for a file.


Copy this code and paste it in your HTML
  1. Public Shared Function GetMimeType(ByVal file As String) As String
  2. Dim mime As String = Nothing
  3. Dim MaxContent As Integer = CInt(New FileInfo(file).Length)
  4. If MaxContent > 4096 Then
  5. MaxContent = 4096
  6. End If
  7.  
  8. Dim fs As New FileStream(file, FileMode.Open)
  9.  
  10. Dim buf(MaxContent) As Byte
  11. fs.Read(buf, 0, MaxContent)
  12. fs.Close()
  13. Dim result As Integer = FindMimeFromData(IntPtr.Zero, file, buf, MaxContent, Nothing, 0, mime, 0)
  14.  
  15. Return mime
  16. End Function
  17.  
  18. <DllImport("urlmon.dll", CharSet:=CharSet.Auto)> _
  19. Private Shared Function FindMimeFromData( _
  20. ByVal pBC As IntPtr, _
  21. <MarshalAs(UnmanagedType.LPWStr)> _
  22. ByVal pwzUrl As String, _
  23. <MarshalAs(UnmanagedType.LPArray, ArraySubType:=UnmanagedType.I1, SizeParamIndex:=3)> ByVal _
  24. pBuffer As Byte(), _
  25. ByVal cbSize As Integer, _
  26. <MarshalAs(UnmanagedType.LPWStr)> _
  27. ByVal pwzMimeProposed As String, _
  28. ByVal dwMimeFlags As Integer, _
  29. <MarshalAs(UnmanagedType.LPWStr)> _
  30. ByRef ppwzMimeOut As String, _
  31. ByVal dwReserved As Integer) As Integer
  32. End Function

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.