所以我想知道如果任何人都可以点我的另一种方法与 更多的MIME类型,或者另一种方法/类是你,我 可以包括MIME类型,我认为合适的。
我用Winista和URLMON的混合检测上传的文件的 real格式..
下载Winista:http://www.netomatix.com/Products/DocumentManagement/MimeDetector.aspx
或者与URLMON下载该项目依傍在这里: https://github.com/MeaningOfLights/MimeDetect
Winista MIME检测
说有人用jpg扩展名重命名exe文件,你仍然可以使用二进制分析来确定“真实”文件格式。它不会检测到swf或flv,但几乎可以完成其他众所周知的格式+您可以使用十六进制编辑器并添加更多可检测的文件。
文件魔术
Winista检测使用包含有关文件类型的信息和用于标识内容type.eg签名的XML文件“的mime-type.xml”真正的MIME类型:
<!--
! Audio primary type
! -->
<mime-type name="audio/basic"
description="uLaw/AU Audio File">
<ext>au</ext><ext>snd</ext>
<magic offset="0" type="byte" value="2e736e64000000"/>
</mime-type>
<mime-type name="audio/midi"
description="Musical Instrument Digital Interface MIDI-sequention Sound">
<ext>mid</ext><ext>midi</ext><ext>kar</ext>
<magic offset="0" value="MThd"/>
</mime-type>
<mime-type name="audio/mpeg"
description="MPEG Audio Stream, Layer III">
<ext>mp3</ext><ext>mp2</ext><ext>mpga</ext>
<magic offset="0" value="ID3"/>
</mime-type>
当Winista失败的检测到真正的文件格式,我已经使出回URLMON方法:
public class urlmonMimeDetect
{
[DllImport(@"urlmon.dll", CharSet = CharSet.Auto)]
private extern static System.UInt32 FindMimeFromData(
System.UInt32 pBC,
[MarshalAs(UnmanagedType.LPStr)] System.String pwzUrl,
[MarshalAs(UnmanagedType.LPArray)] byte[] pBuffer,
System.UInt32 cbSize,
[MarshalAs(UnmanagedType.LPStr)] System.String pwzMimeProposed,
System.UInt32 dwMimeFlags,
out System.UInt32 ppwzMimeOut,
System.UInt32 dwReserverd
);
public string GetMimeFromFile(string filename)
{
if (!File.Exists(filename))
throw new FileNotFoundException(filename + " not found");
byte[] buffer = new byte[256];
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
{
if (fs.Length >= 256)
fs.Read(buffer, 0, 256);
else
fs.Read(buffer, 0, (int)fs.Length);
}
try
{
System.UInt32 mimetype;
FindMimeFromData(0, null, buffer, 256, null, 0, out mimetype, 0);
System.IntPtr mimeTypePtr = new IntPtr(mimetype);
string mime = Marshal.PtrToStringUni(mimeTypePtr);
Marshal.FreeCoTaskMem(mimeTypePtr);
return mime;
}
catch (Exception e)
{
return "unknown/unknown";
}
}
}
从Winista方法中,我退到这里的URLMON:
public MimeType GetMimeTypeFromFile(string filePath)
{
sbyte[] fileData = null;
using (FileStream srcFile = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
byte[] data = new byte[srcFile.Length];
srcFile.Read(data, 0, (Int32)srcFile.Length);
fileData = Winista.Mime.SupportUtil.ToSByteArray(data);
}
MimeType oMimeType = GetMimeType(fileData);
if (oMimeType != null) return oMimeType;
//We haven't found the file using Magic (eg a text/plain file)
//so instead use URLMon to try and get the files format
Winista.MimeDetect.URLMONMimeDetect.urlmonMimeDetect urlmonMimeDetect = new Winista.MimeDetect.URLMONMimeDetect.urlmonMimeDetect();
string urlmonMimeType = urlmonMimeDetect.GetMimeFromFile(filePath);
if (!string.IsNullOrEmpty(urlmonMimeType))
{
foreach (MimeType mimeType in types)
{
if (mimeType.Name == urlmonMimeType)
{
return mimeType;
}
}
}
return oMimeType;
}
Winista from netomatix。 AFAIK是在2000年初基于开源Java项目进行C#重写的。请享用!
你也可以使用注册表方法或中提到的.Net 4.5 method由Paul Zahra链接,但Winista是最好的恕我直言。
UPDATE:
对于桌面应用程序,你可能会发现WindowsAPICodePack效果更好:
using Microsoft.WindowsAPICodePack.Shell;
using Microsoft.WindowsAPICodePack.Shell.PropertySystem;
private static string GetFilePropertyItemTypeTextValueFromShellFile(string filePathWithExtension)
{
var shellFile = ShellFile.FromFilePath(filePathWithExtension);
var prop = shellFile.Properties.GetProperty(PItemTypeTextCanonical);
return prop.FormatForDisplay(PropertyDescriptionFormatOptions.None);
}
我不知道这是你想要的,但你可以从主要的MIME类型的列表IIS。 – din 2013-03-08 18:27:29
但是FindMimeFromData方法被硬编码为26种MIME类型,我无法修改它接受更多的MIME类型。 – 2013-03-08 18:36:21
那么在那种情况下,你可能会找到另一种方式来完成你的任务。如果你能找到你想要读取的数据类型的“扩展名”,那么你可能有更多的机会来确定MIME类型,如果你只是想从读取二进制数据知道MIME类型,那么就我所知,你必须限制为FindMimeFromData方法。 – din 2013-03-08 18:44:33