internet上搜了一下别人的解決方案。不少人說用MIME來判斷。自己也試了一下,如果用.NET的上傳元件,确實可以。但如果用HTML基本的上傳元件卻不行(我是在HTML頁中有上傳元件,POST到背景另外的頁面)。于是自己寫了一段代碼來檢查上傳檔案是否真是圖檔檔案。
前面兩步檢查屬初級檢查(當然,在前台用JS作了用戶端的擴充名檢查),如果通過再使用圖檔類檢查,如果是真是圖檔 就能通過,否則不行(已經過測試)
protected bool isValidImage(System.Web.HttpPostedFile postedFile)
{
string sMimeType = postedFile.ContentType.ToLower();
if (sMimeType.IndexOf("image/") < 0)
return false;
if (postedFile.ContentLength < 50)
return false;
try
{
System.Drawing.Image img = System.Drawing.Image.FromStream(postedFile.InputStream);
if (img.Width * img.Height < 1)
return false;
img.Dispose();
}
catch
{
return false;
}
return true;
}
轉載于:https://www.cnblogs.com/dodui/archive/2011/12/02/2272819.html