JS 获取上传文件大小的方法

第一种方法
function getFileSize(filePath)   
{   
    var fso = new ActiveXObject("Scripting.FileSystemObject");   
    alert("文件大小为:"+fso.GetFile(filePath).size);   
}   
</script>   
<body>   
<INPUT TYPE="file" NAME="file" SIZE="30" οnchange="getFileSize(this.value);">   
</body>  

第二种方法
<script type="text/javascript">   
function getFileSize(filePath)   
{   
    var image = new Image();   
    image.dynsrc = filePath;   
    alert(image.fileSize);   
}   
</script>   
<body>   
<INPUT TYPE="file" NAME="file" SIZE="30" οnchange="getFileSize(this.value)">   
</body>  

第一种方法会报“Automation” 的错误。在IE8下载需要允许运行。

第二种方法在IE6下面可以,在IE8下面会报"无法设置dynsrc属性.拒绝访问.“错误,如果用大写的DYNSRC,就不会报错,但是后面的fileSize就不能用了。


相关文章:http://www.csharpwin.com/dotnetspace/12652r1167.shtml

原文地址:http://hi.baidu.com/_feiying/blog/item/dac89227fc52a008908f9d0e.html/cmtid/78efc59bb20d36bbc8eaf445


限制:<input type="text" size="4" value="10" name="fileSizeLimit" id="fileSizeLimit"/> K
<input type="file" name="file1" id="file1" size="40" οnchange="changeSrc(this)"/>
<br>
<img src=http://blog.xunuo.com/blog/images/icons/23.gif id="fileChecker" alt="test"/>

<script type="text/javascript">
var oFileChecker = document.getElementById("fileChecker");

function changeSrc(filePicker)
{
    oFileChecker.src = filePicker.value;
}

oFileChecker.onreadystatechange = function ()
{
    if (oFileChecker.readyState == "complete")
    {
        checkSize();
    }
}

function checkSize()
{
    var limit  = document.getElementById("fileSizeLimit").value * 1024;

    if (oFileChecker.fileSize > limit)
    {
        alert("too large");
    }
    else
    {
        alert("ok");
    }
}
</script>  

上面方法有其可取之处


相关文章:http://www.csharpwin.com/dotnetspace/12652r1167.shtml

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值