ValidateFileExtention.aspx Font Size:
Source Code Viewer Files: ValidateFileExtention.aspx   ValidateFileExtention.aspx.vb   
Config File: Web.Config   
CSS File: ExamplesStyle.css   
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="ValidateFileExtention.aspx.vb" Inherits="EAUploadExamples.ValidateFileExtention"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>Validate file extention</title>
        <link href="ExamplesStyle.css" rel="stylesheet">
        <script>
            function ValidateFileExtention(extentions)
            {
                var messageBox = document.getElementById('ValidationMessage');
                                
                if (document.getElementById('UploadingFile').value == '')                
                {
                    messageBox.innerHTML = 'Please select a file!';
                    return false;
                }
                else if (!Validate(document.getElementById('UploadingFile').value, extentions))
                {
                    messageBox.innerHTML = 'Type of selected file is not allowed. Please select a correct file!';
                    return false;
                }
                
                return true;
            }
            
            function Validate(fileName, extentions)
            {
                var allowExtentions = extentions.split(';');
                
                for (i = 0; i < allowExtentions.length; i++)
                {
                    if (EndsWith(fileName, allowExtentions[i]))
                    {
                        return true;
                    }
                }
                
                return false;
            
            }
            
            function EndsWith(sourceString, matchString)
            {
                var endOfSourceString = sourceString.substring(sourceString.length - matchString.length);
                
                if (endOfSourceString.toLowerCase() != matchString.toLowerCase())
                {
                    return false;
                }
                
                return true;            
            }
        </script>
    </HEAD>
    <body>
        This example demonstrates how to validate an extention of selected file before 
        uploading it to the server.
        <br>
        <br>
        Uploaded file will be stored to the "UploadedFiles" folder that is located in 
        the root directory of an examples.<br>
        You should grant write access to "UploadedFiles" folder to the ASP.NET process 
        identity(typically {MACHINE}\ASPNET on IIS 5<BR>
        or Network Service on IIS 6).
        <br>
        <br>
        <form id="Form1" method="post" runat="server" enctype="multipart/form-data" onsubmit="return ValidateFileExtention('jpg;jpeg;gif;png');">
            Only files with ".jpg", ".jpeg", ".gif" or ".png" extentions are allowed.
            <br>
            <div id="ValidationMessage" style="COLOR: #ff0000">&nbsp;</div>
            <table style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none">
                <tbody>
                    <tr>
                        <td>
                            <input type="file" id="UploadingFile" name="UploadingFile" size="40">
                        </td>
                    </tr>
                    <tr>
                        <td align="right">
                            <br>
                            <asp:Button id="Button1" runat="server" Width="80px" Text="Upload"></asp:Button>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br>
            <asp:Label id="lblResponse" runat="server"></asp:Label>
        </form>
        <hr>
        <a href="default.aspx" title="All examples">Other examples</a>
        <p>
            Used files: Bin\EasyAlgo.EAUpload.dll, web.config, ValidateFileExtention.aspx, 
            ValidateFileExtention.aspx.vb, ExamplesStyle.css [Optional]
        </p>
    </body>
</HTML>