Validating Image / File size on FileUpload before uploading file/ image using Java script


In this article, we are solving the problem regarding the upload image / file size before file upload. We are Validating Image / File size on FileUpload before uploading file/ image using Java script

Source Code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="sizevaildation.aspx.cs" Inherits="sizevaildation" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>File size validation using JavaScript</title>
    <script type="text/javascript">
        function validatefileuploadersize() {
            var uploadControl = document.getElementById('<%= FileUpload1.ClientID %>');
            if (uploadControl.files[0].size > 1048576) {
                document.getElementById('DvErrorMsg').style.display = "block";
                return false;
            }
            else {
                document.getElementById('DvErrorMsg').style.display = "none";
                return true;
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
        <fieldset style="width: 350px;">
            <legend>File Size Validation </legend>
            <asp:FileUpload ID="FileUpload1" runat="server" onchange="validatefileuploadersize();" />
            <div id="DvErrorMsg" style="background-color: Red; color: White; width: 290px; padding: 3px;
                display: none;">
                Please Choose file size less than size 1 MB
            </div>
        </fieldset>
    </div>
    </form>
</body>
</html>



 Out Put:-

Validating Image / File size on FileUpload before uploading file/ image using Java script Validating Image / File size on FileUpload before uploading file/ image using Java script Reviewed by NEERAJ SRIVASTAVA on 9:36:00 PM Rating: 5

No comments:

Powered by Blogger.