public
Last active

Get file size of file in file input control.

  • Download Gist
getFileSize
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// file: file input control e.g. $("#file")[0]
function getFileSize(file)
{
if (0 === file.value.length)
return 0;
 
// Non-IE browsers
if (file.files)
{
if (file.files.length > 0)
return Math.floor(file.files[0].size / 1024); // size in KB
else
return 0;
}
else // IE
{
try
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fo = fso.getFile(file.value);
return Math.floor(fo.size / 1024); // size in KB
}
catch (e)
{
// ActiveX must be enabled in your IE browser.
return 0;
}
}
}

go to internetoptions < security < customlevel < initialize and script active x controls not marked safe for scripting and mark them enabled

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.