Home > Web Front-end > JS Tutorial > body text

JS获取文件大小方法小结_javascript技巧

WBOY
Release: 2016-05-16 15:27:06
Original
1910 people have browsed it

本文实例总结了JS获取文件大小方法。分享给大家供大家参考,具体如下:

方法一,利用ActiveX控件实现:






Copy after login

这个方法在IE可以用,不足之处会有安全提示,把文件名改为.hta则会屏蔽掉安全提示。

方法二,利用img的dynsrc属性:

代码:




Copy after login

这个方法在IE6可以用,在IE7,IE8,Firefox,chrome不能用。

方法三,利用img的fileSize:

代码:

 
 
图片来源 本地远程:
预览区
Copy after login

在IE,FireFox,chrome都可以用,不过只判断图片文件的大小。

附:js判断文件格式及大小

//判断照片大小
function getPhotoSize(obj){
  photoExt=obj.value.substr(obj.value.lastIndexOf(".")).toLowerCase();//获得文件后缀名
  if(photoExt!='.jpg'){
    alert("请上传后缀名为jpg的照片!");
    return false;
  }
  var fileSize = 0;
  var isIE = /msie/i.test(navigator.userAgent) && !window.opera;      
  if (isIE && !obj.files) {     
     var filePath = obj.value;      
     var fileSystem = new ActiveXObject("Scripting.FileSystemObject");  
     var file = fileSystem.GetFile (filePath);        
     fileSize = file.Size;     
  }else { 
     fileSize = obj.files[0].size;   
  } 
  fileSize=Math.round(fileSize/1024*100)/100; //单位为KB
  if(fileSize>=10){
    alert("照片最大尺寸为10KB,请重新上传!");
    return false;
  }
}

Copy after login

jsp页面:

复制代码 代码如下:

希望本文所述对大家JavaScript程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!