for client side javascript
<script type="text/javascript">
var validFilesTypes=["bmp","gif","png","jpg","jpeg","doc","xls"];
function ValidateFile()
{
var file = document.getElementById("<%=FileUpload1.ClientID%>");
var label = document.getElementById("<%=Label1.ClientID%>");
var path = file.value;
var ext=path.substring(path.lastIndexOf(".")+1,path.length).toLowerCase();
var isValidFile = false;
for (var i=0; i<validfilestypes.length;> {
if (ext==validFilesTypes[i])
{
isValidFile=true;
break;
}
}
if (!isValidFile)
{
label.style.color="red";
label.innerHTML="Invalid File. Please upload a File with" +
" extension:\n\n"+validFilesTypes.join(", ");
}
return isValidFile;
}
</script></script>
code behind file
protected void btnUpload_Click(object sender, EventArgs e)
{
string[] validFileTypes={"bmp","gif","png","jpg","jpeg","doc","xls"};
string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
bool isValidFile = false;
for (int i = 0; i < validFileTypes.Length; i++)
{
if (ext == "." + validFileTypes[i] )
{
isValidFile = true;
break;
}
}
if (!isValidFile)
{
Label1.ForeColor = System.Drawing.Color.Red;
Label1.Text = "Invalid File. Please upload a File with extension " +
string.Join(",", validFileTypes);
}
else
{
Label1.ForeColor = System.Drawing.Color.Green;
Label1.Text = "File uploaded successfully.";
}
}