<%@ Page language="c#" Codebehind="ValidateFileExtention.aspx.cs" AutoEventWireup="True" 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/>
<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"> </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" onclick="Button1_Click"></asp:Button>
</td>
</tr>
</tbody>
</table>
<br />
<asp:Label id="lblResponse" runat="server"></asp:Label>
</form>
<hr />
<a target="_blank" href="http://www.easyalgo.com/quickstart/util/srcview.aspx?path=/Examples/EAUpload/ValidateFileExtention.src">View C Sharp source code</a>
<a target="_blank" href="http://www.easyalgo.com/quickstart/util/srcview.aspx?path=/Examples/EAUpload/VBNET/ValidateFileExtention.src">View VB NET source code</a>
</body>
</HTML>