我有一个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服务器上使用此代码,我还会遇到任何问题吗? 谢谢。