自知其無(wú)知
          不懂,慢慢懂。
          posts - 2,comments - 2,trackbacks - 0
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
          <html xmlns="http://www.w3.org/1999/xhtml">
          <head>
          <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
          <title>無(wú)標(biāo)題文檔</title>
          <script>

           UpLoadFileCheck=function()
           {  
             this.AllowExt=".jpg,.gif";//允許上傳的文件類型 0為無(wú)限制 每個(gè)擴(kuò)展名后邊要加一個(gè)"," 小寫字母表示
             this.AllowImgFileSize=0;//允許上傳文件的大小 0為無(wú)限制 單位:KB
             this.AllowImgWidth=0;//允許上傳的圖片的寬度 0為無(wú)限制 單位:px(像素)
             this.AllowImgHeight=0;//允許上傳的圖片的高度 0為無(wú)限制 單位:px(像素)
             this.ImgObj=new Image();
             this.ImgFileSize=0;
             this.ImgWidth=0;
             this.ImgHeight=0;
             this.FileExt="";
             this.ErrMsg="";
             this.IsImg=false;//全局變量
              
           }
           
           UpLoadFileCheck.prototype.CheckExt=function(obj)
           {
            this.ErrMsg="";
            this.ImgObj.src=obj.value;
            //this.HasChecked=false;  
            if(obj.value=="")
            {
             this.ErrMsg="\n請(qǐng)選擇一個(gè)文件";    
            }
            else
            {   
             this.FileExt=obj.value.substr(obj.value.lastIndexOf(".")).toLowerCase();
             if(this.AllowExt!=0&&this.AllowExt.indexOf(this.FileExt)==-1)//判斷文件類型是否允許上傳
             {
              this.ErrMsg="\n該文件類型不允許上傳。請(qǐng)上傳 "+this.AllowExt+" 類型的文件,當(dāng)前文件類型為"+this.FileExt;   
             }
            }
            if(this.ErrMsg!="")
            {
             this.ShowMsg(this.ErrMsg,false);
             return false;
            }
            else   
             return this.CheckProperty(obj);    
           }
           
           UpLoadFileCheck.prototype.CheckProperty=function(obj)
           {
            if(this.ImgObj.readyState!="complete")//
             {
              sleep(1000);//一秒使用圖能完全加載    
             }   
             
            if(this.IsImg==true)
            {
             this.ImgWidth=this.ImgObj.width;//取得圖片的寬度
             this.ImgHeight=this.ImgObj.height;//取得圖片的高度
             if(this.AllowImgWidth!=0&&this.AllowImgWidth<this.ImgWidth)
             this.ErrMsg=this.ErrMsg+"\n圖片寬度超過(guò)限制。請(qǐng)上傳寬度小于"+this.AllowImgWidth+"px的文件,當(dāng)前圖片寬度為"+this.ImgWidth+"px";
           
             if(this.AllowImgHeight!=0&&this.AllowImgHeight<this.ImgHeight)
             this.ErrMsg=this.ErrMsg+"\n圖片高度超過(guò)限制。請(qǐng)上傳高度小于"+this.AllowImgHeight+"px的文件,當(dāng)前圖片高度為"+this.ImgHeight+"px"; 
            }
           
            this.ImgFileSize=Math.round(this.ImgObj.fileSize/1024*100)/100;//取得圖片文件的大小  
            if(this.AllowImgFileSize!=0&&this.AllowImgFileSize<this.ImgFileSize)
             this.ErrMsg=this.ErrMsg+"\n文件大小超過(guò)限制。請(qǐng)上傳小于"+this.AllowImgFileSize+"KB的文件,當(dāng)前文件大小為"+this.ImgFileSize+"KB";
           
            if(this.ErrMsg!="")
            {
             this.ShowMsg(this.ErrMsg,false);
             return false;
            }
            else
             return true;
           } 
           
           UpLoadFileCheck.prototype.ShowMsg=function(msg,tf)//顯示提示信息 tf=false 顯示錯(cuò)誤信息 msg-信息內(nèi)容
           {
            /*msg=msg.replace("\n","<li>");
            msg=msg.replace(/\n/gi,"<li>");
             */
            alert(msg);
           }
           function   sleep(num)  
             {  
              var   tempDate=new   Date();  
              var   tempStr="";  
              var   theXmlHttp   =   new   ActiveXObject(   "Microsoft.XMLHTTP"   );  
              while((new   Date()-tempDate)<num   )  
              {  
              tempStr+="\n"+(new   Date()-tempDate);  
              try{  
              theXmlHttp   .open(   "get",   "about:blank?JK="+Math.random(),   false   );  
              theXmlHttp   .send();  
              }  
              catch(e){;}  
              }  
             //containerDiv.innerText=tempStr;  
             return;  
             }  

           function c(obj)
           {
            var d=new UpLoadFileCheck();
            d.IsImg=true;
            d.AllowImgFileSize=100;
            d.CheckExt(obj)
           }
          </script>

          </head>

          <body>
          <input name="" type="file"   onchange="c(this)"/>


          </body>

          下載源程序:
          </html>

          posted on 2008-12-12 00:08 CopyHoo 閱讀(1113) 評(píng)論(0)  編輯  收藏 所屬分類: JavaScript
          主站蜘蛛池模板: 都兰县| 达尔| 新乡县| 来宾市| 沐川县| 昌黎县| 涪陵区| 兴城市| 兰考县| 开封县| 洪雅县| 正安县| 白河县| 台北市| 叙永县| 绥德县| 石河子市| 融水| 克拉玛依市| 上杭县| 武义县| 突泉县| 台中县| 门头沟区| 大石桥市| 沽源县| 柞水县| 高州市| 安泽县| 腾冲县| 洪洞县| 儋州市| 喀喇沁旗| 奇台县| 五原县| 南安市| 林芝县| 兴海县| 乳山市| 浠水县| 绥江县|