これはpostを確認できます。私は試しました..
// declare FindMimeFromData from urlmon.dll
[DllImport(@"urlmon.dll", CharSet = CharSet.Unicode)]
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.IntPtr ppwzMimeOut,
System.UInt32 dwReserverd
);
// you can return boolen value if mime type is not image
public static string getMimeFromFile(IFormFile file)
{
byte[] buffer = new byte[256];
using (Stream fs = file.OpenReadStream())
{
if (fs.Length >= 256)
fs.Read(buffer, 0, 256);
else
fs.Read(buffer, 0, (int)fs.Length);
}
try
{
//System.UInt32 mimetype;
IntPtr mimetype = Marshal.AllocHGlobal(256);
FindMimeFromData(0, null, buffer, 256, null, 0, out mimetype, 0);
//System.IntPtr mimeTypePtr = new IntPtr(mimetype);
string mime = Marshal.PtrToStringUni(mimetype);
Marshal.FreeCoTaskMem(mimetype);
return mime;
}
catch (Exception e)
{
return "unknown/unknown";
}
}
[HttpGet]
public ActionResult Test()
{
ViewData["Message"] = "Your test page.";
return View();
}
[HttpPost]
public ActionResult Test(IFormFile file)
{
string mimeType = getMimeFromFile(file);
ViewData["Message"] = mimeType;
return View();
}
とTest.cshtml
<h3>@ViewData["Message"]</h3>
<form action="/Home/Test" method="post" enctype="multipart/form-data">
<input name="file" type="File"> <br>
<input type="submit" value="submit">
</form>
UPDATE:
あなたはthisをチェックし、クロスプラットフォーム用のDLLのインポートを使用している場合カント。 hereから新しいMIMEタイプを追加できます。
http://stackoverflow.com/questions/3643750/net-image-libraries –