Upload differnt types of files to different folders using Switch Case

Me_Himanshu
Posted by in ASP.NET category on for Beginner level | Points: 250 | Views : 8455 red flag
Rating: 4.33 out of 5  
 3 vote(s)

One of the Junior at working was checking with me on how to use Switch Case in FileUpload control.He was assigned a task where different types files should be uploaded to the respective folder.for example,if the user is uploading image,it should be uploaded to the image folder,if PDF then to PDF folder ,if docx/doc then to word folder.And moreover the main part was if the file with the same name already exists in that folder,then filename must get renamed and a notification with new filename is shown to the user.When I helped him complete the code,I thought of sharing it too.


 Download source code for Upload differnt types of files to different folders using Switch Case

Introduction


This article explains using Switch case with File Upload Control to upload files in to the respective folder.Also,if the same name file exists in the folder,then file name gets changed.

Using the code


Have a fileupload control ,a button to upload files  and two label controls on the page. Please create different folders in your solution explorer for respective files upload. for eg,image forlder for image files,wordfiles folder for uploading word files and PDF folder for uploading PDF files.

Check the code after the image



// protected void Button1_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            uploadfiles(FileUpload1.PostedFile);
        }
        else
        {
            Label1.Text = "please select file to upload";
        }

    }
protected void uploadfiles(HttpPostedFile file)
{ string ext=Path.GetExtension(FileUpload1.FileName); //get the extension of the file string filepath; switch(ext) { case ".jpg": filepath=Server.MapPath("./uploadimage/"); for image files ViewState["filepath"] = filepath.ToString();//we store filepath in viewstate break; case ".png": filepath = Server.MapPath("./uploadimage/"); ViewState["filepath"] = filepath.ToString(); break; case ".docx": filepath = Server.MapPath("./uploadwordfiles/"); for word files// ViewState["filepath"] = filepath.ToString(); break; case ".doc": filepath = Server.MapPath("./uploadwordfiles/"); ViewState["filepath"] = filepath.ToString(); break; case ".pdf": filepath = Server.MapPath("./uploadpdf/");//for pdf files ViewState["filepath"] = filepath.ToString(); break; case ".jpeg": filepath = Server.MapPath("./uploadimage/"); ViewState["filepath"] = filepath.ToString(); break; default: Label1.Text = "this file type is not allowed"; return; }
       // In this part of code, we will check if the  file with the same name exists in the folder.if yes,then we will execute a while loop,changing the name of the file//
       string filename = FileUpload1.FileName;
       string checkpath= ViewState["filepath"].ToString() + filename;
       string tempfilepath = "";
        if(File.Exists(checkpath))
{ int counter = 2; while(File.Exists(checkpath))
{ tempfilepath = counter.ToString() + filename; checkpath= ViewState["filepath"].ToString() + tempfilepath;
counter++; } filename = tempfilepath; label2.Text = ext.ToString(); Label1.Text = "this filename already exists.your file is saved with the filename" + " " + filename.ToString(); } else { // Notify the user that the file was saved successfully. Label1.Text = "Your file was uploaded successfully."; } ViewState["filepath"]+= filename; FileUpload1.SaveAs(checkpath);
}

Using the code above, we can decide the file path according to the file type and also change the file name if the file with the same name already exists.

Conclusion


We learned on how to use Switch Case in FileUpload control


Page copy protected against web site content infringement by Copyscape

About the Author

Me_Himanshu
Full Name: Himanshu Pandey
Member Level: Starter
Member Status: Member
Member Since: 6/6/2012 2:28:47 AM
Country: India
Himanshu Pandey
not yet
I am a avid user if this website since i started my career ,always fantasize to post some useful articles into it.I am having 2.5 years of experience in Asp.Net,C#,Sql server,JavaScript and just started m working on WCF and Linq.

Login to vote for this post.

Comments or Responses

Posted by: Rajarajah on: 1/30/2013 | Points: 25
Realy nice and Usefull Article

Login to post response

Comment using Facebook(Author doesn't get notification)