your question is incomplete or having less information.which control you used.
i want to give you different ways:-
1]use file uploader controller with regular epression:-
<asp:FileUpload ID="flUpld" runat="server" />
<asp:RegularExpressionValidator
id="RegularExpressionValidator1" runat="server"
ErrorMessage="Only PDF files are allowed!"
ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.pdf|.PDF)$"
ControlToValidate="flUpld" CssClass="text-red"></asp:RegularExpressionValidator>
2] Use ajax file uploader controller:-
<ajaxtoolkit:ajaxfileupload id="AjaxFileUpload1" xmlns:ajaxtoolkit="#unknown">
ThrobberID="myThrobber"
ContextKeys="fred"
AllowedFileTypes="jpg,pdf"
MaximumNumberOfFiles=10
runat="server"/></ajaxtoolkit:ajaxfileupload>
3] or use javascript script to validate file is .pdf
<script type ="text/javascript">
var validFilesTypes=["PDF","pdf"];
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; i++)
{
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>
this is simple file upload code you have to modify this code as per your requirement
protected void UploadButton_Click(object sender, EventArgs e)
{
if(FileUploadControl.HasFile)
{
try
{
string filename = Path.GetFileName(FileUploadControl.FileName);
FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
StatusLabel.Text = "Upload status: File uploaded!";
}
catch(Exception ex)
{
StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
}
}
}
or you can use server side validation and code is here. but i recommend to use client side validation and simple uploading code-
make change in below code as per your requirement:-
protected void btnUpload_Click(object sender, EventArgs e)
{
string FilePath = ConfigurationManager.AppSettings["FilePath"].ToString();
bool blSucces = false;
string filename = string.Empty;
if (FileUploadToServer.HasFile)
{
try
{
string[] allowdFile = { ".pdf" };
string FileExt = System.IO.Path.GetExtension(FileUploadToServer.PostedFile.FileName);
bool isValidFile = allowdFile.Contains(FileExt);
if (!isValidFile)
{
lblMsg.ForeColor = System.Drawing.Color.Red;
lblMsg.Text = "Please upload only pdf ";
}
else
{
int FileSize = FileUploadToServer.PostedFile.ContentLength;
if (FileSize <= 1048576)
{
filename = Path.GetFileName(FileUploadToServer.FileName);
FileUploadToServer.SaveAs(Server.MapPath(FilePath) + filename);
lblMsg.Text = "File upload successfully!";
blSucces = true;
}
else
{
lblMsg.Text = "Attachment file size should not be greater then 1 MB!";
}
}
}
catch (Exception ex)
{
lblMsg.Text = "Error occurred while uploading a file: " + ex.Message;
}
}
else
{
lblMsg.Text ="Please select a file to upload.";
}
if (blSucces)
{
Updatefileinfo(filename, FilePath + filename);
}
}