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


Top 5 Contributors of the Month
david stephan

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

Responses
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";

break;

case ".png":

contenttype = "image/png";

break;

case ".gif":

contenttype = "image/gif";

break;

default:
contenttype = "";
break;
}


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;
}else{}



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,
GowthamManju
Chennai



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

First Check with extension and then Filename :

eg:
string extension= system.io.path.getextension(fileupload1.filename);
if(extension == ".xlsx" || extension == ".xls")
{
string filename=system.io.path.getfilenamewithoutextension(fileupload1.filename);
if(filename=="solution_week_date")
{
fileupload1.saveas(server.mappath("~/Files/"+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")
{
fileupload1.saveas(server.mappath("~/Files/"+fileupload1.filename));
response.write("File Uploaded");
}
else
{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.
FileUpload1.SaveAs("PathWhereYouWantToUploadFile");
}
}
}


Regards
Shiva


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
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

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