ITPub博客

首页 > Linux操作系统 > Linux操作系统 > c#验证照片

c#验证照片

原创 Linux操作系统 作者:iDotNetSpace 时间:2009-11-06 11:29:58 0 删除 编辑
           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;
                

            }

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-618276/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5765891