0

我有一个 VB.net 代码来检查文件的 mime 类型并交叉检查 mime 文件类型及其扩展名以验证文件。它在许多机器上运行良好,但在某些机器上运行不正常。我使用了 urlmon.dll 的函数 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

另外,如果我在 Linux 服务器上使用此代码,我会遇到任何问题吗?谢谢。

4

0 回答 0