Yasserzaid’s Weblog

June 1, 2009

Validate FileUpload Extension

Filed under: ASP.Net, Javascript — yasserzaid @ 6:17 am

Hi

try this example to Validate FileUpload Extension using Javascript

<html xmlns=”http://www.w3.org/1999/xhtml” >
<head id=”Head1″ runat=”server”>
    <title>Untitled Page</title>
    <script type =”text/javascript”>
    var validFiles=["bmp","gif","png","jpg","jpeg"];
        function OnUpload()
        {
          var obj = document.getElementById(“<%=FileUpload1.ClientID%>”);
          var source=obj.value;
          var ext=source.substring(source.lastIndexOf(“.”)+1,source.length).toLowerCase();
          for (var i=0; i<validFiles.length; i++)
          {
            if (validFiles[i]==ext)
                break;
          }
          if (i>=validFiles.length)
          {
            alert(“This not a valid file upload file with an extention of one of the following:\n\n”+validFiles.join(“, “));
            return false;
          }
          return true;
         }
    </script>
</head>
<body>
    <form id=”form1″ runat=”server”>
    <div>
        <asp:FileUpload ID=”FileUpload1″ runat=”server” />
        <asp:Button ID=”Button1″ runat=”server” Text=”Button” OnClientClick =”return OnUpload();”  />
    </div>
    </form>
</body>
</html>

Hope this helps

Refrence her

Good Luck

4 Comments »

  1. Hi Yasser,
    I have no objection if you use my code but give reference to the article.
    Thanks

    Comment by Mudassar Khan — June 3, 2009 @ 6:47 pm

  2. Thanks

    Mudassar

    Comment by Mudassar Khan — June 4, 2009 @ 3:30 am

  3. You can delete these comments
    I am writing here since I cannot see About page to contact you

    Thanks

    Comment by Mudassar Khan — June 4, 2009 @ 3:35 am

  4. Dear Friends,

    I hope you are doing well. I have launched a web site http://www.codegain.com and it is basically aimed C#,JAVA,VB.NET,ASP.NET,AJAX,Sql Server,Oracle,WPF,WCF and etc resources, programming help, articles, code snippet, video demonstrations and problems solving support. I would like to invite you as an author and a supporter. Looking forward to hearing from you and hope you will join with us soon.

    Please forward this email to all of your friends who are related IT. Send to us your feed about site also.

    Thank you
    RRaveen
    Founder CodeGain.com

    Comment by RRaveen — July 2, 2009 @ 2:39 pm


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.