Hi rajath r,
Following is your solution ,
But don't forget to mark my answer as accepted if it helped you :)
And if you want to upload .gif file then change folllowing condition
if (Extension == "jpg" || Extension == "JPG" || Extension == "JPEG" || Extension == "jpeg")
Aspx code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function ValidateFileUpload(Source, args)
{
var fuData = document.getElementById('<%= fileUploadImage.ClientID %>');
var FileUploadPath = fuData.value;
if(FileUploadPath =='')
{
// There is no file selected
args.IsValid = true;
}
else
{
var Extension = FileUploadPath.substring(FileUploadPath.lastIndexOf('.') + 1).toLowerCase();
if (Extension == "jpg" || Extension == "JPG" || Extension == "JPEG" || Extension == "jpeg")
{
args.IsValid = true; // Valid file type
}
else
{
args.IsValid = false; // Not valid file type
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Image ID="imgShow" runat="server" />
<asp:FileUpload ID="fileUploadImage" runat="server" size="10" />
<asp:Button ID="btnUpload" runat="server" onclick="btnUpload_Click" Text="Upload" />
<asp:CustomValidator runat="server" ID="cv" ClientValidationFunction='ValidateFileUpload' ControlToValidate='fileUploadImage' ErrorMessage='You can upload only jpg/jpeg file' ></asp:CustomValidator>
</div>
</form>
</body>
</html>
C# code
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnUpload_Click(object sender, EventArgs e)
{
if (fileUploadImage.FileName != "")
{
String fullpath = Server.MapPath(fileUploadImage.FileName);
fileUploadImage.SaveAs(fullpath);
imgShow.ImageUrl = fileUploadImage.FileName;
}
}
}