Get File Size Using jQuery

How to check file size before uploading using jQuery

Get File Size Using jQuery All Browsers
function GetFileSize(fileid) {
    try {
        var fileSize = 0;
        //for IE
        if ($.browser.msie) {
            //before making an object of ActiveXObject, 
            //please make sure ActiveX is enabled in your IE browser
            var objFSO = new ActiveXObject("Scripting.FileSystemObject");
            var filePath = $("#" + fileid)[0].value;
            var objFile = objFSO.getFile(filePath);
            var fileSize = objFile.size; //size in kb
            fileSize = fileSize / 1048576; //size in mb 
        }
        //for FF, Safari, Opeara and Others
        else {
            fileSize = $("#" + fileid)[0].files[0].size //size in kb
            //fileSize = fileSize / 1048576; //size in mb 
        }

        alert(GetSize(fileSize));
        //alert("Uploaded File Size is " + fileSize + " MB");
    } catch (e) {
        alert("Error is :" + e);
    }
}

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

    return ((Math.round(fSize * 100) / 100) + ' ' + fSExt[i]);
    //alert((Math.round(fSize * 100) / 100) + ' ' + fSExt[i]);
}
Get File Size Using jQuery Get File Size Using jQuery Reviewed by Bhaumik Patel on 8:23 PM Rating: 5