c#验证照片
Code
if (Request.Files == null || Request.Files.Count == 0)
{
this.lblMsg.Text = msg2;
this.lblMsg.ForeColor = Color.Red;
return false;
}
HttpPostedFile file = Request.Files[0];
//检查是否为空
if (file == null || file.ContentLength == 0)
{
this.lblMsg.Text = msg2;
this.lblMsg.ForeColor = Color.Red;
return false;
}
//检查是否是图片
if (file.ContentType.ToLower().IndexOf("image") < 0)
{
// Response.Redirect(Globals.AppendQuerystring(url, "msg", "2", false), true);
this.lblMsg.Text = msg2;
this.lblMsg.ForeColor = Color.Red;
return false;
}
//检查是否是允许上传的类型bmp,jpg,png
FileInfo info = new FileInfo(file.FileName);
string supportedImgType = "^.png$|^.jpg$|^.bmp$";
Regex reg = new Regex(supportedImgType, RegexOptions.Compiled);
if (string.IsNullOrEmpty(info.Extension) || !reg.IsMatch(info.Extension.ToLower()))
{
string imgType = "";
if (string.IsNullOrEmpty(info.Extension))
imgType = "未知";
else
imgType = info.Extension;
this.lblMsg.Text = string.Format(msg5, imgType);
this.lblMsg.ForeColor = Color.Red;
return false;
}
//检查是否超出大小
if ((file.ContentLength / 1024) / 1024 > 5)
{
this.lblMsg.Text = msg6;
this.lblMsg.ForeColor = Color.Red;
return false;
}
if (Request.Files == null || Request.Files.Count == 0)
{
this.lblMsg.Text = msg2;
this.lblMsg.ForeColor = Color.Red;
return false;
}
HttpPostedFile file = Request.Files[0];
//检查是否为空
if (file == null || file.ContentLength == 0)
{
this.lblMsg.Text = msg2;
this.lblMsg.ForeColor = Color.Red;
return false;
}
//检查是否是图片
if (file.ContentType.ToLower().IndexOf("image") < 0)
{
// Response.Redirect(Globals.AppendQuerystring(url, "msg", "2", false), true);
this.lblMsg.Text = msg2;
this.lblMsg.ForeColor = Color.Red;
return false;
}
//检查是否是允许上传的类型bmp,jpg,png
FileInfo info = new FileInfo(file.FileName);
string supportedImgType = "^.png$|^.jpg$|^.bmp$";
Regex reg = new Regex(supportedImgType, RegexOptions.Compiled);
if (string.IsNullOrEmpty(info.Extension) || !reg.IsMatch(info.Extension.ToLower()))
{
string imgType = "";
if (string.IsNullOrEmpty(info.Extension))
imgType = "未知";
else
imgType = info.Extension;
this.lblMsg.Text = string.Format(msg5, imgType);
this.lblMsg.ForeColor = Color.Red;
return false;
}
//检查是否超出大小
if ((file.ContentLength / 1024) / 1024 > 5)
{
this.lblMsg.Text = msg6;
this.lblMsg.ForeColor = Color.Red;
return false;
}