<HTML>
<HEAD>
<SCRIPT>
function getFileSize (fileName) {
 if (document.all) {
    window.oldOnError = window.onerror;
    window.onerror = function (err) {
      if (err.indexOf('utomation') != -1) {
        alert('file access not possible');
        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>
<BODY>
<FORM NAME="formName">
<INPUT TYPE="file" NAME="fileName">
<BR>
<INPUT TYPE="button" VALUE="check file size"
       ONCLICK="alert(getFileSize(this.form.fileName.value))"
>
</FORM>
</BODY>
</HTML>

解决方案 »

  1.   

    net_lover(孟子E章):我原前,也遇到过这个问题,就没有解决,试一下你上面的代码,选择完文件之后,都提示:'file access not possible'。
    如果,能解决这个问题,我可以另开贴给分100
      

  2.   

    <script>
    function GetFileSize()
    {
    var filename=document.all.File.value;
    if(filename=='')
    {
    return false;
    }
    try
    {
    var ado_stream=new ActiveXObject("ADODB.Stream");
    //1=adTypeBinary;2=adTypeText
      ado_stream.Type=2;
      ado_stream.Open();
      ado_stream.LoadFromFile(filename);//将文件信息存入流
    alert((ado_stream.Size/1024).toFixed(2)+"KB")
    }
    catch(e)
    {
    window.confirm(e);
    return false;
    }
    return true;
    }
    </script>
    <input type=file id="File"><input type=button onclick="GetFileSize()">
      

  3.   

    <script language="Jscript">
    function getFileSize(strFileName)
    {
            var objStream = new ActiveXObject("ADODB.Stream");
            objStream.Type = 1;
            objStream.Open();
            objStream.LoadFromFile(strFileName);
            return( Math.round(objStream.Size/1024,2) + "KB");
    }
    </script>
    <input type="file" name="ad"  onchange="alert(getFileSize(this.value))">