Wednesday 23 October 2013

How to upload multiple file in c# | asp.net | Created directory of file name

How to upload multiple file in asp.net with directory creation.
In this article we are going to learn how to upload multiple files in asp.net with c# code.
We can also find out if file has already uploaded then there will be directory creation with existing file name.
Lets take a examples ..
If i want to upload my resume file and there is three resume so what i need to do is , first of all i will select my resume 1

then 2 and last 3..
while uploading 3 files the resume1.doc , resume2.doc , resume3.doc will be selected at same time same names directory will

be created ..
but if you have already added or uploaded any of resume out of this then it will inform you that file already exists.

import using System.IO; namespace.

.aspx page



  <tr><td><asp:FileUpload runat="server" ID="fp1" /></td></tr>
  <tr><td><asp:FileUpload runat="server" ID="fp2" /></td></tr>
  <tr><td><asp:FileUpload runat="server" ID="fp3" /></td></tr>



now code to .cs page.


  private void Fileupload()
        {
            if (fp1.HasFile)
                {
                   
                   
                    string filename = fp1.FileName;
                  string str=  FunchekFilealreadythere(filename);
                  String filePath = Server.MapPath(@"~/upload_resume/"+str+"/"+ fp1.FileName);
                 fp1.SaveAs(filePath);//will add file name in file name directory.
            }

                if (fp2.HasFile)
                {
                 string filename1 = fp2.FileName;
                 string str1 = FunchekFilealreadythere(filename1);
                 String filePath1 = Server.MapPath(@"~/upload_resume/" + str1 + "/" + fp2.FileName);
                 fp2.SaveAs(filePath1);

                }


                if (fp3.HasFile)
                {
                    string filename2 = fp3.FileName;
                    string str2 = FunchekFilealreadythere(filename2);
                    String filePath2 = Server.MapPath(@"~/upload_resume/" + str2 + "/" + fp3.FileName);
                    fp3.SaveAs(filePath2);

                }


           
            else
            {
                Response.Write("uploaded");
            }
        }

        public string    FunchekFilealreadythereg filename)
        {
            string nm=filename ;
            string[] fnm=new string [1];
            fnm=nm .Split ('.');//will just take file name not extension.


            if (!System.IO.Directory.Exists(Server.MapPath(@"~/upload_resume/" + fnm[0] + "")))
            {
                System.IO.Directory.CreateDirectory(Server.MapPath(@"~/upload_resume/" + fnm [0]+ ""));
            }
            return fnm[0];
           
        }

here i have shown how to upload three files and create directory as well .
You can upload multiple files and create directory  how much you need.
Share This
Previous Post
Next Post

FYJC XI standard online admisson Process and declaraton of Merit list . Cut off List For prevous year also . 10 Th Results onlne declaraton Maharashtra Region .

0 comments: