Click here to Skip to main content
15,867,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
heyy...


How to limit the file size in ajax file upload control .I tried to search in internet ..but I couldn't found...Any idea about that or any example...plz... or any javascript for limt th e file size....
Posted
Updated 4-Oct-13 22:04pm
v2
Comments
JoCodes 5-Oct-13 5:04am    
I dont feel it can be done through JS . How about using a asp fileupload control and check the contentlength property?

 
Share this answer
 
Comments
aravindnass 5-Oct-13 5:12am    
thankkss.....:)I will check....
you can not limit in ajax after you response to user you check size if size need to limit message to user with java script
 
Share this answer
 
Comments
aravindnass 5-Oct-13 3:44am    
How can I limit file size with javascript code ...Can u plz send me or any example..??
JavaScript
function checkfilesize() {
    var total_filesize_num = 0;
    var myElements = $(".filesize");
    if (myElements.length == 0) {
        $(".ajax__fileupload_uploadbutton").css("visibility", "hidden");
        return;
    }
    for (var i = 0; i < myElements.length; i++) {              
        var filesize = myElements.eq(i).html(); //$(".filesize").html();     
        total_filesize_num = total_filesize_num + filesize_tonum(filesize);
    }
    if (total_filesize_num > 5) {
        $(".ajax__fileupload_uploadbutton").css("visibility", "hidden");
        alert('Maximum file size is 5MB only! Please select another one.');
        return;
    } else {
        $(".ajax__fileupload_uploadbutton").css("visibility", "visible");
    }
}

function countsumfilesize() {
    var sumfilesize = 0;
    var myElements = $(".filesize");
    for (var i = 0; i < myElements.length; i++) {
        alert(myElements.eq(i).html());
    }
}

function filesize_tonum(filesize) {
    var filesize_num = 0;
    if (filesize.indexOf("kb") > 0) {
        var space = filesize.lastIndexOf(" ");
        filesize_num = parseFloat("0." + filesize.substr(0, filesize.length - space + 1));
    }
    else if (filesize.indexOf("MB") > 0) {
        var space = filesize.lastIndexOf(" ");
        filesize_num = parseFloat(filesize.substr(0, filesize.length - space + 1));
    }    
    return filesize_num;
}


$(".ajax__fileupload_dropzone").bind("drop", function () {
        checkfilesize();
    });
    
$(".ajax__fileupload_queueContainer").bind("click", function () {       
        checkfilesize();
    });
    
$(".ajax__fileupload_uploadbutton").bind("mouseenter", function () {
        checkfilesize();
    });
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900