[javascript] 用 js 检测上传文件大小

原文地址: http://www.dotblogs.com.tw/cross/archive/2010/09/21/17840.aspx

 

google了很久,基本上都是用 activeX 来实现~至于为什么不行,这个不多说,说一下以下方法:

 

已经测试通过的浏览器:IE6+,firefox,chrome,其中 firefox 和 chrome 要能支持 HTML5。

 

< html >
< head >
     < meta  http-equiv ="Content-Type"  content ="text/html; charset=big5" >
     < title >上传 </ title >
</ head >
< body >
     < form  action ="XXXXXX"  method ="POST"  name ="FileForm"  enctype ="multipart/form-data" >
     < div  align ="center" >
        图片:
         < input  type ="file"  name ="file1"  size ="20"  id ="file1"   />
         < input  type ="button"  onclick ="checkFile()"   /></ div >
     </ form >
</ body >
</ html > 

 

<script src="http://code.jquery.com/jquery-1.4.2.min.js" type="text/javascript"></script>

<script language="JavaScript" type="text/javascript">
     var fileSize = 0;  // 文件大小
     var SizeLimit = 1024;   // 上传上限,单位:byte

     function checkFile() {
         var f = document.getElementById("file1");
         // FOR IE
         if ($.browser.msie) {
             var img =  new Image();
            img.onload = checkSize;
            img.src = f.value;
        }
         // FOR Firefox,Chrome
         else {
            fileSize = f.files.item(0).size;
            checkSize();
        }
    }

     // 检查文件大小
     function checkSize() {
         // FOR IE FIX
         if ($.browser.msie) {
            fileSize =  this.fileSize;
        }

         if (fileSize > SizeLimit) {
            alert('文件超过大小');
        }  else {
            document.FileForm.submit();
        }
    }
</script>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值