3

I have used ActiveXObject for IE browser but it is not working. I have got the file size for other browsers in js but unable to getting the file size for IE browser. I have used the following code for this:-

if ($.browser.msie==true)
 {
     var fileSystemObject = new ActiveXObject("Scripting.FileSystemObject");
     var path = document.uploadDocumentForm.documentUpload.value;
     var file = fileSystemObject.getFile(path);
     var size = file.size;
     alert(size);

/*  var a = document.getElementById(fileId).value;
            $('#myImage').attr('src',a);
            var imgbytes = document.getElementById('myImage').fileSize;       
            alert("here"+imgbytes);
            var imgkbytes = Math.round(parseInt(imgbytes)/1024);        */

}
else
{
               var fileInput = $("#"+fileId)[0]; 
               var imgbytes = fileInput.files[0].size; 
              var imgkbytes = Math.round(parseInt(imgbytes)/(1024));
}   

Can anyone help me to get the filesize for IE browsers. I have gone through all the ideas but unable to get filesize for IE browser. Please provide the idea or code for this...

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
user1414979
  • 92
  • 4
  • 11
  • The fileinput's value is a *fakepath*, I don't think you could get a FileSystem object from it... – Bergi May 24 '12 at 12:16

1 Answers1

0

A suggestion that works is following.

<script type="text/javascript">
function AlertFilesize(){
    if(window.ActiveXObject){
        var fso = new ActiveXObject("Scripting.FileSystemObject");
        var filepath = document.getElementById('fileInput').value;
        var thefile = fso.getFile(filepath);
        var sizeinbytes = thefile.size;
    }else{
        var sizeinbytes = document.getElementById('fileInput').files[0].size;
    }

    var fSExt = new Array('Bytes', 'KB', 'MB', 'GB');
    fSize = sizeinbytes; i=0;while(fSize>900){fSize/=1024;i++;}

    alert((Math.round(fSize*100)/100)+' '+fSExt[i]);
}
</script>

<input id="fileInput" type="file" onchange="AlertFilesize();" />

It was taken from another answer Get file size before uploading .

Community
  • 1
  • 1
P. R. Ribeiro
  • 2,999
  • 1
  • 19
  • 20