我整好了一个,单独访问可以获得文件大小,但是如果部署到web工程下访问此界面就获取不了文件大小。
我的代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  <html> 
  <head> 
  <meta http-equiv="Content-Type" content="text/html; charset=GBK"> 
  <title>Insert title here </title> 
  <script type="text/javascript"> 
     var isIE = /msie/i.test(navigator.userAgent) && !window.opera; 
   
     var sizeLabel = ["B", "KB", "MB", "GB"]; 
       
     function fileChange(target) { 
   
         var fileSize = 0; 
           
         if (isIE && !target.files) { 
             var filePath = target.value; 
             var fileSystem = new ActiveXObject("Scripting.FileSystemObject");    
             var file = fileSystem.GetFile (filePath); 
             fileSize = file.Size; 
         } else { 
             fileSize = target.files[0].size; 
         } 
         displayFileSize(fileSize); 
     } 
   
     function displayFileSize(size) { 
         var fileSize = document.getElementById("fileSize"); 
 alert("文件大小为:"+calFileSize(size));
         fileSize.innerHTML = calFileSize(size); 
     } 
   
     function calFileSize(size) { 
         for (var index = 0; index  < sizeLabel.length; index++) { 
               
             if (size  < 1024) { 
                 return round(size, 2) + sizeLabel[index]; 
             } 
               
             size = size / 1024; 
         } 
   
         return round(size, 2) + sizeLabel[index]; 
     } 
   
     function round(number, count) { 
         return Math.round(number * Math.pow(10, count)) / Math.pow(10, count); 
     }   </script> 
  </head> 
  <body> 
      <div> 
          <input type="file" onchange="fileChange(this);"> 
      </div> 
      <div id="fileSize"></div> 
  </body> 
  </html>

解决方案 »

  1.   

    你的这个放在服务器上是指定获取不成功的,本地获取还可以,因为服务器获取的图片路径会在服务器本地查找这个文件,可以想象是找不到的。
    这个问题以前项目中也遇到过,在ie6下的解决方案可参考http://www.cnblogs.com/poissonnotes/archive/2010/03/04/1678288.html,
    不过当时项目中用到的是ie8,上面的方式会弹出允许组件,太不友好,最后没有办法只是在页面控件上方给出了友情提示:上传附件:xxxx不能超过xxM