faqts : Computers : Programming : Languages : JavaScript : Forms : File Upload

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

198 of 233 people (85%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

Can I check the size of a selected file before uploading?
Can I check the dimensions of a selected image before uploading?
Is there a way to get rid of this ActiveX alert at the first time your running this script ?

May 12th, 2008 21:14
i can do it, Martin Honnen, Johnny Kristiansen,


Checking a selected image's dimensions is possible with IE4+ (and 
probably with NN3 though I haven't tried), simply be creating an
  Image
object and checking its dimensions.
NN4 had a security hole by allowing http: loaded pages to open local 
file: urls thus nowadays you can't from a web page create an Image 
object with a local file: url src unless your script is trusted to do 
so.
See
http://www.faqts.com/knowledge-base/view.phtml/aid/840/fid/125/lang/en
on running trusted scripts with NN4.
Checking a selected file's size requires local file system access which 
is usually for web pages not possible due to security considerations. 
If security settings are low enough NN4 can call into Java to read the 
file's size, IE4+ can use the FileSystemObject to check the file's size.
The following page has two functions
  getFileSize 
and
  getImageDimension 
containing NN4 and IE4+ code to solve the described tasks.
<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>
http://www.stupidarticles.com
http://www.halazona.com
http://www.shikapika.com
http://www.stakoza.com
http://www.uploadarticles.com
http://www.ganazat.com
http://www.damima.com
http://www.tarabiza.com
http://www.articlesxarticles.com
http://www.articlesfreedirectory.com