JS验证文件大小

ASP.NET中自带的上传控件如果文件很大的话通常会死机,如果能在客户端验证的话,就很好了。于是从网上东拼西凑个点JS。

 

 

ExpandedBlockStart.gif 代码
// 验证浏览器
         // 添加人:冯俊杰
         // 添加日期:2010-03-30
         var   Sys  =  {};
        
if (navigator.userAgent.indexOf( " MSIE " ) > 0 ) {  
            Sys.ie
= true ;
        }   
        
if (isFirefox = navigator.userAgent.indexOf( " Firefox " ) > 0 ){   
            Sys.firefox
= true ;
        }
        
if (isFirefox = navigator.userAgent.indexOf( " 'Chrome " ) > 0 ){   
            Sys.firefox
= true ;
        }
         
if (isFirefox = navigator.userAgent.indexOf( " 'Opera " ) > 0 ){   
            Sys.firefox
= true ;
        }
        
// 清空上传文件大小超过5M给提示不让上传
         // 添加人:冯俊杰
         // 添加日期:2010-03-30
         function  checkFileChange(obj) {
            
var  filesize  =   0 ;
            
if (Sys.firefox){
                filesize 
=  obj.files[ 0 ].fileSize;
            }
else   if (Sys.ie){
                
var  filePath  =  obj.value;
                
var  image = new  Image();   
                image.dynsrc
= filePath;   
                filesize
= image.fileSize;    
            }
            
if  (filesize > 5 * 1024 * 1024 ) // 大于5M
            {
              window.alert(
' More than 5M,Please Try again! ' );
              clearFileInput(obj);
              
return   false ;
            }
        }
        
// 清空验证后的fileupload控件内容
         // 添加人:冯俊杰
         // 添加日期:2010-03-30
     function  clearFileInput(file){
        
var  form = document.createElement( ' form ' );
        document.body.appendChild(form);
        
var  pos = file.nextSibling;
        form.appendChild(file);
        form.reset();
        pos.parentNode.insertBefore(file,pos);
        document.body.removeChild(form);
}
 
应用:
 
 

 

< asp:FileUpload  ID ="FileUpload1"  runat ="server"  onchange ="return checkFileChange(this)"   />

 

 

转载于:https://www.cnblogs.com/jack-feng/archive/2010/03/30/1700825.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值