counting filesize with javascript
https://developer.mozilla.org/en-US/docs/Using_files_from_web_applications#Example.3A_Showing_file%28s%29_size
by lalatino
HTML
<form name="form1">
<input type="file" name="ifile" onchange="updateSize();" multiple />
</form>
<span id="totalSize"></span>
JavaScript
// http://stackoverflow.com/questions/4226288/if-condition-if-the-browser-is-ie-and-ie-browser-version-is-older-than-9
function getIEVersion() {
var rv = -1; // Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer') {
var ua = navigator.userAgent;
var re = new RegExp(/MSIE ([0-9]{1,}[\.0-9]{0,})/);
if (re.test(ua) !== null) {
rv = parseFloat(RegExp.$1);
}
}
return rv;
}
function updateSize() {
var ver = getIEVersion();
//alert(ver);
if (ver != -1 && ver < 10.0) {
return;
}
var nBytes = 0,
oFiles = document.forms.form1.ifile.files,
nFiles = oFiles.length,
i, sOutput, aMultiples, nApprox;
for (i = 0; i < nFiles; i++) {
nBytes += oFiles[i].size;
}
sOutput = nBytes + " bytes";
// optional code for multiples approximation
for (aMultiples = ["KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"], i = 0, nApprox = nBytes / 1024; nApprox > 1; nApprox /= 1024, i++) {
sOutput = nApprox.toFixed(3) + " " + aMultiples[i] + " (" + nBytes + " bytes)";
}
// end of optional code
document.getElementById("totalSize").innerHTML = 'Total size: ' + sOutput;
}