Thursday, May 9, 2013

Asp .Net File Upload validate file extension using JavaScript

==>> In this article I am posting how we can validate Asp .NET file upload control for file extension using Java Script. for this we need create a java script function...




JAVA SCRIPT FUNCTION:-

 function validateExtensipon() {
        
            var arrayExt = ['pdf', 'doc', 'docx', 'txt', 'xlsx', 'ppt', 'zip'];

            var FilesVale = document.getElementById("FileUpload1");

            var Ext = FilesVale.value.substring(FilesVale.value.lastIndexOf('.') + 1).toLowerCase();

            if (arrayExt.indexOf(Ext) <= -1) {

                alert("Upload only pdf,doc,zip,txt.xlsx and ppt extension flle");

                return false;

            }

            else {
           
            alert("File Upload Successfully..........")
            }

        }



This is the function that we have used for check extension of file

Aspx Page Code



<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>

    <script type="text/javascript">

        function validateExtensipon() {
         
            var arrayExt = ['pdf', 'doc', 'docx', 'txt', 'xlsx', 'ppt', 'zip'];

            var FilesVale = document.getElementById("FileUpload1");

            var Ext = FilesVale.value.substring(FilesVale.value.lastIndexOf('.') + 1).toLowerCase();

            if (arrayExt.indexOf(Ext) <= -1) {

                alert("Upload only pdf,doc,zip,txt.xlsx and ppt extension flle");

                return false;

            }

            else {
           
            alert("File Upload Successfully..........")
            }

        }

    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <br />
        <br />
    </div>
    <asp:Button ID="Button1" runat="server" Text="Upload" OnClientClick="return validateExtensipon();" />
    </form>
</body>
</html>


Asp .Net File upload





Download sample code 

1 comment: