關於js獲取上傳檔案大小處理原理是利用了js讀取當前的圖片,然後再創建一個new image再把圖片的大小讀取出來,然後判斷當前檔圖片的大小是不是指定大小就ok了。
<script language="網頁特效">
function showsize(files)
{
var fso,f;
fso=new activexobject("scripting.filesystemobject");
f=fso.getfile(files);
var filesize = f.size ;
if((filesize/1024) < 1024){
alert(filesize/1024+"k");
}else if( ((filesize/1024) > 1024) && ((filesize/(1024*1024)) < 1024)){
alert(filesize/(1024*1024)+"m");
}else{
alert(filesize/(1024*1024*1024)+"g")
}
}
</script>
方法二
:<input type="text" size="4" value="10" name="filesizelimit" id="filesizelimit"/> k
<input type ="file" name="file1" id="file1" size="40" onchange="changesrc(this)"/>
<img src="about:blank" id=" filechecker" alt="test" height="18"/>
<script type="text/javascript">
var ofilechecker = document.getelementbyid("filechecker");
function changesrc(filepicker)
{
ofilechecker.src = filepicker.value;
}
ofilechecker.onreadystatechange = function ()
{
if (ofilech ecker.readystate == "complete")
{
checksize();
}
}
function checksize()
{
var limit = document.getelementbyid("filesizelimit").value * 1024;
if (ofilechecker.filesize > limit)
{
alert("too large");
}
else
{
alert("ok");
}
}
</script>
方法三js獲取上傳檔案大小
<script type="text/javascript"><!--
function getfilesize1(filename){
var size = null;
var file = null;
if(filename){
var fso = new activexobject("scripting.filesystemobject");
try{
file=fso.getfile(filename);
size=file.size;
}
catch(e){
return("get file error.");
}
}
return(size);
}
-->
</script>
<div>
<input id="upfile" type="file" value="upload" />
<button value="get size" onclick="var msg=getfilesize(document.getelementbyid('upfile').value);alert(msg);" / >
</div>