FindMimeFromData function giving incorrect result in some machine

240 Views Asked by At

I have a VB.net code to check mime type of a file and cross check the mime file type with it's extension to validate the file. It is working fine on many machines but not working on some. I have used urlmon.dll's function FindMimeFromData.

1) ```VB.net

Private Shared Function FindMimeFromData(ByVal pBC As IntPtr,
<MarshalAs(UnmanagedType.LPWStr)> ByVal pwzUrl As String,
<MarshalAs(UnmanagedType.LPArray, ArraySubType:=UnmanagedType.I1, SizeParamIndex:=3)> ByVal pBuffer As Byte(), ByVal cbSize As Integer,
<MarshalAs(UnmanagedType.LPWStr)> ByVal pwzMimeProposed As String, ByVal dwMimeFlags As Integer, <Out()> ByRef ppwzMimeOut As IntPtr, ByVal dwReserved As Integer) As Integer
End Function
2) ```VB.net
 Public Shared Function ScanFileForMimeType(fileName As HttpPostedFile, SaveLocation As String) As String
        Dim mimeout As IntPtr
        If Not System.IO.File.Exists(SaveLocation) Then
            Throw New FileNotFoundException(SaveLocation + " not found")
        End If
        Dim MaxContent As Integer = CInt(New FileInfo(SaveLocation).Length)
        If MaxContent > 4096 Then
            MaxContent = 4096
        End If

        Dim fs As New FileStream(SaveLocation, FileMode.Open)

        Dim buf(MaxContent) As Byte
        fs.Read(buf, 0, MaxContent)
        fs.Close()
        Dim result As Integer = FindMimeFromData(IntPtr.Zero, SaveLocation, buf, MaxContent, Nothing, 0, mimeout, 0)

        If result <> 0 Then
            'Throw Marshal.GetHRForExceptionresult)
        End If

        Dim mime As String = Marshal.PtrToStringUni(mimeout)
        Marshal.FreeCoTaskMem(mimeout)
        Return mime
    End Function

Also, will I face any problem if I use this code on Linux server? Thanks.

0

There are 0 best solutions below