File Size restriction validation using Javascript

 

Introduction: Hello Friends!! In this article I will explain about the implementation of File size restriction using Javascript with complete Javascript code. This code Validate the File Size before uploading the file using JavaScript as per your requirement

 

Implementation:The following HTML code will use a HTML Fileuploader Control and a Button with  named Upload File and also  a lable with id named  display_msg. Here I will assign a javascript OnClick event handler to the Button.

When the button named Upload File is clicked Validation of  javascript  function will be called. Inside the created Validation javascript function, a condition is performed that if the size of the file that will be upload is less than allowed size then file will be uploaded and other side if not then, the error message will be displayed in lable element.

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

<lable id="lbl_message" style="color: red;"></ lable >

<br/>

<input type="button_upload" value="Upload File" onclick="return Validate()" />

<scripttype="text/javascript">

    function Validate() {

        var file = document.getElementById("file_upload");

        var msg = document.getElementById("lbl_message");

        msg.innerHTML = "";

        var size = parseFloat(file.files[0].size);

        var maxSizeKB = 20; //here you can specify the maximum file size of file that you want to upload  in KB.

        var maxSize = maxSizeKB * 1024; //File size is returned in Bytes.

        if (size > maxSize) {

            msg.innerText = "Max file size " + maxSizeKB + "KB allowed.";

            file.value = "";

            return false;

        }

    }

</script>

Conclusion: In above code, I explained that how we can create File Size restriction validation using Javascript. This code is very helpful for every developer. Bye Bye everyone and take care of you Developers. We will come back shortly with the new article.

 

Regards

Using Asp.net

 

Comments

Popular posts from this blog

Sending reset password link for one time use only in asp.net

add delete update inside gridview using store procedure in ASP.NET

Change password using asp.net