如何在JS中判断文件大小

是这样的,我用上传组件上传文件,表单如下:  
  <INPUT   TYPE="FILE"   SIZE="20"   NAME="file1"   class="inputtext">  
   
  当用户选取文件,点击上传按钮后,我想在未启动上传程序之前,先判断文件大小,如果超过1M,就不让上传

<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()">
posted @ 2007-08-04 23:01  赖文华.NET  阅读(2495)  评论(3编辑  收藏  举报