Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get file size from clientside using javascript in IE?

I used the following methode

HTML

<input type="file" id="loadfile" />

JavaScript

var file = document.getElementById('loadfile').files[0];
alert( "name " +  file.name + "Size " +  file.size );

It works fine other browsers except IE :( How to get in IE ?

like image 933
raki Avatar asked Nov 22 '12 11:11

raki


People also ask

How do I get the size of a file in HTML?

The id="size" element will be used to display the size of selected file from the id="upload" element. We listen on the change event of the file input, and get the selected files via e.

How do you find the file size?

Right-click the file and click Properties. The image below shows that you can determine the size of the file or files you have highlighted from in the file properties window. In this example, the chrome. jpg file is 18.5 KB (19,032 bytes), and that the size on disk is 20.0 KB (20,480 bytes).

What unit is file size in JavaScript?

Javascript – The File Size Is Measured In Bytes With Code Examples.


3 Answers

IE doesn't supply file size information I'm afraid. You might be able to use HTML5 File API with IE10, see here:-

Javascript to check filesize before upload in Internet Explorer

like image 178
Lloyd Avatar answered Oct 02 '22 00:10

Lloyd


you can do it like this using activeX

function getSize()
{
 var myFSO = new ActiveXObject("Scripting.FileSystemObject");
 var filepath = document.upload.file.value;
 var thefile = myFSO.getFile(filepath);
 var size = thefile.size;
 alert(size + " bytes");
}

see here for more detail;

how validate file size using HTML and Javascript on client side

like image 24
rahul Avatar answered Oct 02 '22 01:10

rahul


document.getElementById('loadfile').addEventListener('change', checkFile, false);

function checkFile(e) {
    var file_list = e.target.files;
    for (var i = 0, file; file = file_list[i]; i++) {
        var fileExtension = file.name.split('.')[file.name.split('.').length - 1].toLowerCase();
        var iConvert = (file.size / 1024).toFixed(2);

        txt = "File type : " +fileExtension + "\n";
        if(file.size > (1024 * 1024)){
            txt += "Size: " + (file.size / (1024*1024)).toFixed(2) + " MB \n";
        } else {
        txt += "Size: " + (file.size / 1024).toFixed(2) + " KB \n";
        }
        alert(txt);
    }
}

see filddle

like image 26
Harish Singh Avatar answered Oct 02 '22 00:10

Harish Singh