js判断上传文件大小

<!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>js判断上传文件大小</title>
</head>

<body>
//方法一

<input type="file" name="file1" onchange="showsize(this.value)">
<script language="网页特效">
<!--
function showsize(files)
{
var fso,f;
fso=new activexobject("scripting.filesystemobject");
f=fso.getfile(files);
if(f.size>1024*4){
alert(f.size+" bytes");
}
}
//-->
</script>
方法二 js判断上传文件大小

<script type="text/javascript">
function chksize(strfilename){
try
{
var maxsize = 100; //定义允许文件的大小,单位kb,请根据需要自行修改!
var objstream = new activexobject("adodb.stream");
objstream.type = 1;
objstream.open();
objstream.loadfromfile(strfilename);
if(math.round(objstream.size/1024)>maxsize)
{alert ("抱歉!您选择的文件为 "+math.round(objstream.size/1024,2)+" kb n超过了程序"+maxsize+" k(www.111cn.net)b 的限制!");
document.form1.reset();
return false;
}
else
alert("可以上传");
}catch(e)
{
alert("不支持");
}
}
</script><
form name="form1" action="" method="post">
<input type="file" name="upfile" onchange="chksize(this.value)" />
//方法三

<script>

function getfilesize (filename) {

if (document.all) {
window.oldonerror = window.onerror;
window.onerror = function (err) {
if (err.indexof('utomation') != -1) {
alert('没有访问文件的权限');
return true;
}
else
return false;
};
var fso = new activexobject('scripting.filesystemobject');
var file = fso.getfile(filename);
window.onerror = window.oldonerror;
return file.size; }}
</script>
</head>

<form name="formname"><input type="file" name="filename"><br>
<input type="button" value="查看文件字节大小" onclick="alert(getfilesize(this.form.filename.value))"><br>
</form>

</body>
</html>
from:http://www.111cn.net/wy/js-ajax/34564.htm

posted @ 2014-10-20 22:10  divor  阅读(1568)  评论(1编辑  收藏  举报