WEB开发网
开发学院网页设计JavaScript 用javaScript获取文件,jpg图片大小 阅读

用javaScript获取文件,jpg图片大小

 2010-09-14 13:17:12 来源:WEB开发网   
核心提示:<HTML><HEAD><SCRIPT>function getImageDimension (imgURL, loadHandler) {var img = new Image();img.onload = loadHandler;if (document.layers&& loc

<HTML>
<HEAD>
<SCRIPT>
function getImageDimension (imgURL, loadHandler) {
 var img = new Image();
 img.onload = loadHandler;
 if (document.layers
   && location.protocol.toLowerCase() != 'file:'
   && navigator.javaEnabled())
  netscape.security.PrivilegeManager.enablePrivilege(
   'UniversalFileRead'
  );
 img.src = imgURL;
}
function getFileSize (fileName) {
 if (document.layers) {
  if (navigator.javaEnabled()) {
   var file = new java.io.File(fileName);
   if (location.protocol.toLowerCase() != 'file:')
    netscape.security.PrivilegeManager.enablePrivilege(
    'UniversalFileRead'
    );
   return file.length();
  }
  else return -1;
 }
 else 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;
 }
}
function showImageDimensions () {
 alert(this.width + 'x' + this.height);
}
</SCRIPT>
<SCRIPT>
function checkImageDimensions (fileName) {
 var imgURL = 'file:///' + fileName;
 getImageDimension(imgURL, showImageDimensions);
}
</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))"
>
<BR>
<INPUT TYPE="button" VALUE="check image dimensions"
    ONCLICK="checkImageDimensions(this.form.fileName.value)"
>
</FORM>
</BODY>
</HTML>

Tags:javaScript 获取 文件

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接