function
getFileSize(fileObj) {
if
(document.all) {
window.oldOnError = window.onerror;
window.onerror =
function
(err) {
if
(err.indexOf(
'utomation'
) != -1) {
alert(
'No access to the file permissions.'
);
return
true
;
}
else
return
false
;
};
var
fso =
new
ActiveXObject(
'Scripting.FileSystemObject'
);
var
file = fso.GetFile(fileName);
window.onerror = window.oldOnError;
return
file.Size;
}
}
<input type="file" id="fileText"> <input type="button" value="檢查文件大小" onclick="checkFileChange(document.getElementById('fileText'));"> <script type="text/javascript"> var Sys = {}; if(navigator.userAgent.indexOf("MSIE")>0) { Sys.ie=true; } if(isFirefox=navigator.userAgent.indexOf("Firefox")>0) { Sys.firefox=true; } function checkFileChange(obj) { var filesize = 0; if(Sys.firefox) { filesize = obj.files[0].fileSize; }else if(Sys.ie) { var fileobject = new ActiveXObject ("Scripting.FileSystemObject");//獲取上傳文件的對(duì)象 var file = fileobject.GetFile (document.getElementById("fileText").value);//獲取上傳的文件 var filesize = file.Size;//文件大小 } alert(filesize); } </script>
<script language="JavaScript"> <!-- 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 (ofilechecker.readystate == "complete") { checksize(); }} function checksize(){ var limit = document.getelementbyid("filesizelimit").value * 1024; if (ofilechecker.filesize > limit) { alert("too large"); } else { alert("ok"); }}</script>
<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" onchange="getFileSize(this.value)"> </body>
聯(lián)系客服