获得上传文件的大小

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Show File Data</title>
<style type='text/css'>
body {
    font-family: sans-serif;
}
</style>
<script type='text/javascript'>
function showFileSize() {
    var input, file;
    // (Can't use `typeof FileReader === "function"` because apparently

    // it comes back as "object" on some browsers. So just see if it's there

    // at all.)

    if (!window.FileReader) {

        bodyAppend("p", "The file API isn't supported on this browser yet.");

        return;

    }




    input = document.getElementByIdx_x('fileinput');

    if (!input) {

        bodyAppend("p", "Um, couldn't find the fileinput element.");

    }

    else if (!input.files) {

        bodyAppend("p", "This browser doesn't seem to support the `files` property of file inputs.");

    }

    else if (!input.files[0]) {

        bodyAppend("p", "Please select a file before clicking 'Load'");

    }

    else {

        file = input.files[0];

        bodyAppend("p", "File " + file.name + " is " + file.size + " bytes in size");

    }

}




function bodyAppend(tagName, innerHTML) {

    var elm;




    elm = document_createElement_x(tagName);

    elm.innerHTML = innerHTML;

    document.body.a(elm);

}

</script>

</head>

<body>

<form action='#' οnsubmit="return false;">

<input type='file' id='fileinput'>

<input type='button' id='btnLoad' value='Load' οnclick='showFileSize();'>

</form>

</body>

</html>
转自:http://stackoverflow.com/questions/3717793/javascript-file-upload-size-validation
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值