.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Ava Aiden
Imran Ghani
Thomas Shaw

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

validate filename before upload

Posted By: Catherine     Posted Date: June 07, 2011    Points:5   Category :C#
hi i would like to validate file name using spilt. For example solution_week_date.xlsx or xls. solution comes for every file name. but week and date differ. week may 1,2,3 and 4. date may jan01 or feb09......please tell code for this validation. i am using fileupload control after browsing the file when i press upload button the filename should be validate and that file should be copy in my server. please help me

Author: Gowthammanju             
Posted Date: June 07, 2011     Points: 20   

Hi for image based files use can use code as

1) for retriving file name with extension

string filepath = fileupload_img.PostedFile.FileName;
string filename = Path.GetFileName(filepath);
string ext = Path.GetExtension(filename);

2)to validate the file type:

string contenttype = string.Empty;
switch (ext)
case ".jpg":

contenttype = "image/jpg";


case ".png":

contenttype = "image/png";


case ".gif":

contenttype = "image/gif";


contenttype = "";

3) condition to save files in server

if (contenttype != string.Empty)
lbl_attachments.Text = fileupload_img.FileName;
fileupload_img.SaveAs(Server.MapPath("~/planimages/" + lbl_attachments.Text));
Image1.ImageUrl = "~/planimages/" + lbl_attachments.Text;
lbl_attachments.Text = "~/planimages/" + lbl_attachments.Text;

u can just replace with various extension as u like!....

in above coding
lbl_attachments=lable name;
fileupload_img=file uploader name;

feel free to ask any kind of problem ur facing

Thanks & Regards,

Mark if the answer is true
Author: Sasi Prabhu             
Posted Date: June 07, 2011     Points: 20   

First Check with extension and then Filename :

string extension= system.io.path.getextension(fileupload1.filename);
if(extension == ".xlsx" || extension == ".xls")
string filename=system.io.path.getfilenamewithoutextension(fileupload1.filename);
respose.write("File Saved");
else{response.write("Invalid Filename");}
else{response.write("invalid file type");}

Thanks & Regards,
Sasi Prabhu.
Author: Sasi Prabhu             
Posted Date: June 07, 2011     Points: 20   

string filename=fileupload1.filename.tostring()
if(filename=="solution_week_date.xlsx" || filename =="solution_week_date.xls")
response.write("File Uploaded");
{response.write("Invalid File");}

Thanks & Regards,
Sasi Prabhu.
Author: Susan Thampy             
Posted Date: June 09, 2011     Points: 20   
Author: shiv chandra             
Posted Date: June 12, 2011     Points: 20   

protected void BtnUpload_Click(object sender, EventArgs e)
// To check fileupload control has file or not.
if (FileUpload1.HasFile)
// One more condition to check filename.
// Replace your actual condition with Condition
if(Condition == true)
// Now upload file, if your criteria is matched.


Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend