c#驗證照片

iDotNetSpace發表於2009-11-06
           if (Request.Files == null || Request.Files.Count == 0)
c#驗證照片            
{
                
this.lblMsg.Text = msg2;
                
this.lblMsg.ForeColor = Color.Red;
                
return false;
            }

            HttpPostedFile file 
= Request.Files[0];
            
//檢查是否為空
            if (file == null || file.ContentLength == 0)
c#驗證照片            
{
                
this.lblMsg.Text = msg2;
                
this.lblMsg.ForeColor = Color.Red;
                
return false;
            }


            
//檢查是否是圖片
            if (file.ContentType.ToLower().IndexOf("image"< 0)
c#驗證照片            
{
                
// 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()))
c#驗證照片            
{
                
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)
c#驗證照片            
{
                
this.lblMsg.Text = msg6;
                
this.lblMsg.ForeColor = Color.Red;
                
return false;
                

            }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-618276/,如需轉載,請註明出處,否則將追究法律責任。

相關文章