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

<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>

 

你可能感兴趣的:(JavaScript,html,Security,Access)