我有网上COPY了一个上传文件的代码,
但现在我想要在上传的时候判断这个文件的扩展名为.JPG|.GIF|.PNG 
和文件的大小不不大于400K
  
谢谢 ///'检查文件扩展名字  
HttpPostedFile  postedFile  =  files[iFile];  
string  fileName,  fileExtension;  
fileName  =  System.IO.Path.GetFileName(postedFile.FileName);  
if  (fileName  !=  "")  
{  
fileExtension  =  System.IO.Path.GetExtension(fileName);
    ???//在这里决断文件的扩展名是否为图片和文件大小不大于400K postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("refile ); 
}

解决方案 »

  1.   

    文件大小,可以用FILEINFO类来判断
    后缀的问题可以在string类的方法判断,具体自己去查
      

  2.   

    http://www.meimeile.com/blog2/article.asp?id=66
      

  3.   

    if((!System.IO.Path.GetExtension(upfile_boss.PostedFile.FileName).ToLower().Equals(".jpg"))&&(!System.IO.Path.GetExtension(upfile_boss.PostedFile.FileName).ToLower().Equals(".gif"))&&(!System.IO.Path.GetExtension(upfile_boss.PostedFile.FileName).ToLower().Equals(".png")) 

    //上传的文件类型不正确 
    Response.Write("<script language=javascript>alert('上传资料格式不对,请上传jpg|gif|png格式');</script>");
    return; 
      

  4.   

    fileExtension  =  System.IO.Path.GetExtension(fileName);
    这里获取是文件尾缀名
    if  (fileName  !=  "JPG"&&fileName!="GIF"&&fileName!="PNG") 文件大小
    postedFile.Content.Length>=400*1024
      

  5.   

    //上传到文件夹
    System.Web.HttpPostedFile postfile = Request.Files["File1"];
    if ((postfile==null)||(postfile.FileName.Length==0))
    {
    PageLib.PageScript.PageAlert(Response,"请选择图片!");
    this.photoname.Text="";
    this.cs.Checked=false;
    return;
    }
    //取个随机数做照片ID,防止删除时数据库与文件夹不同步
    Random rand = new Random();
    photoid = int.Parse(rand.Next(10000000,99999999).ToString()); string Path=File1.PostedFile.FileName;// 文件路径
    int Size = File1.PostedFile.ContentLength; // 文件大小
    string Type = File1.PostedFile.ContentType; // 文件类型
    Stream ImageStream = File1.PostedFile.InputStream;
    byte[] Content = new byte[Size];
    int Status = ImageStream.Read(Content, 0, Size);
    string newtype = Type.Substring(0,Type.LastIndexOf("/"));

    if(Type.IndexOf("image")>=0)
    {
    if(Status>500000)
    {
    PageLib.PageScript.PageAlert(Response,"照片太大,请重新选择!");
    this.photoname.Text="";
    this.cs.Checked=false;
    return;
    }
    }
    else
    {
    if(Status>10000000)
    {
    PageLib.PageScript.PageAlert(Response,"短片太大,请重新选择!");
    this.photoname.Text="";
    this.cs.Checked=false;
    return;
    }
    }
      

  6.   

    可以在客戶端去做:
    1。檢查扩展名
    if(document.Form1.File.value!="")
    {
     var arr;
     arr=document.Form1.File.value.split(".");
     if(!(arr[arr.length-1]=="gif" || arr[arr.length-1]=="jpg"))
      {
        alert("不符合上傳的類型!");
        return false;
      }
    }2.文件大小不大于400K
    function GetFileSize()
    {
      var fso=new ActiveXObject("Scripting.FileSystemObject");
      var opath=fso.GetFile(document.Form1.File.value);
      if(opath.size>4*1024)
     {
       return false;
     }
      return true;
    }