js判斷上傳檔案大小

來源:互聯網
上載者:User

標籤:http   io   os   ar   java   for   檔案   on   cti   

<!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

js判斷上傳檔案大小

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.