Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
public void FileUpload1()
    {
        if (flpProspectus.HasFile)
        {

            string fileExtension = System.IO.Path.GetExtension(flpProspectus.FileName);
            int fileSize = flpProspectus.PostedFile.ContentLength;
            HttpFileCollection hfc = Request.Files;             
            {
                string[] arr = new string[5];

                for (int i = 0; i < hfc.Count; i++)
                {
                    if (hfc.GetKey(i) == "ctl00$ContentPlaceHolder1$flpProspectus")
                    {
                        HttpPostedFile hpf = hfc[i];
                        if (hpf.ContentLength > 0)
                        {
                            hpf.SaveAs(Server.MapPath("~/College/fileupload3/") + System.IO.Path.GetFileName(hpf.FileName));
                           string filepath = Server.MapPath("~/College/fileupload3/");
                           string pathx = "~/College/fileupload3/" + hpf.FileName;
                            if (i < 5)
                            {
                                arr[i] = pathx;
                                path1 = arr[0]; path2 = arr[1]; path3 = arr[2]; path4 = arr[3]; path5 = arr[4];

                            }

                        }
                    }
                }
            }
        }      
    }

    public void FileUpload2()
    {
        if (flpOrientation.HasFile)
        {
            string fileExtension = System.IO.Path.GetExtension(flpOrientation.FileName);
            int fileSize = flpOrientation.PostedFile.ContentLength;
            HttpFileCollection hfc = Request.Files;
            string[] arr = new string[5];
            for (int i = 0; i < hfc.Count; i++)
            {
                if (hfc.GetKey(i) == "ctl00$ContentPlaceHolder1$flpOrientation")
                {
                    HttpPostedFile hpf = hfc[i];
                    if (hpf.ContentLength > 0)
                    {
                        hpf.SaveAs(Server.MapPath("~/College/fileupload4/") + System.IO.Path.GetFileName(hpf.FileName));
                        string filepath = Server.MapPath("~/College/fileupload4/");
                        string pathy = "fileupload4/" + hpf.FileName;
                        if (i < 5)
                        {
                            arr[i] = pathy;
                            path6 = arr[0]; path7 = arr[1]; path8 = arr[2]; path9 = arr[3]; path10 = arr[4];

                        }

                    }
                }
            }
        }
    }
    public void FileUpload3()
    {
        if (flpEnquiry.HasFile)
        {
            string fileExtension = System.IO.Path.GetExtension(flpEnquiry.FileName);
            int fileSize = flpEnquiry.PostedFile.ContentLength;
            HttpFileCollection hfc = Request.Files;
            string[] arr = new string[5];
            for (int i = 0; i < hfc.Count; i++)
            {
                if (hfc.GetKey(i) == "ctl00$ContentPlaceHolder1$flpEnquiry")
                {
                    HttpPostedFile hpf = hfc[i];
                    if (hpf.ContentLength > 0)
                    {
                        hpf.SaveAs(Server.MapPath("~/College/fileupload5/") + System.IO.Path.GetFileName(hpf.FileName));
                        string filepath = Server.MapPath("~/College/fileupload5/");
                        string path = "fileupload5/" + hpf.FileName;
                        if (i < 5)
                        {
                            arr[i] = path;
                            path11 = arr[0]; path12 = arr[1]; path13 = arr[2]; path14 = arr[3]; path15 = arr[4];
                        }
                    }

                }
            }
        }

    }
    public void FileUpload4()
    {
        if (flpPunishment.HasFile)
        {
            string fileExtension = System.IO.Path.GetExtension(flpPunishment.FileName);
            int fileSize = flpPunishment.PostedFile.ContentLength;
            HttpFileCollection hfc = Request.Files;
            string[] arr = new string[5];
            for (int i = 0; i < hfc.Count; i++)
            {
                if (hfc.GetKey(i) == "ctl00$ContentPlaceHolder1$flpPunishment")
                {
                    HttpPostedFile hpf = hfc[i];
                    if (hpf.ContentLength > 0)
                    {
                        hpf.SaveAs(Server.MapPath("~/College/fileupload6/") + System.IO.Path.GetFileName(hpf.FileName));
                        string filepath = Server.MapPath("~/College/fileupload6/");
                        string path = "fileupload6/" + hpf.FileName;
                        if (i < 5)
                        {
                            arr[i] = path;
                            path16 = arr[0]; path17 = arr[1]; path18 = arr[2]; path19 = arr[3]; path20 = arr[4];
                        }
                    }

                }
            }
        }

    }
    public void FileUpload5()
    {
        if (flpnooffir.HasFile)
        {
            string fileExtension = System.IO.Path.GetExtension(flpnooffir.FileName);
            int fileSize = flpnooffir.PostedFile.ContentLength;
            HttpFileCollection hfc = Request.Files;
            string[] arr = new string[9];
            for (int i = 0; i < hfc.Count; i++)
            {
                if (hfc.GetKey(i) == "ctl00$ContentPlaceHolder1$flpnooffir")
                {
                    HttpPostedFile hpf = hfc[i];
                    if (hpf.ContentLength > 0)
                    {
                        hpf.SaveAs(Server.MapPath("~/College/fileupload7/") + System.IO.Path.GetFileName(hpf.FileName));
                        string filepath = Server.MapPath("~/College/fileupload7/");
                        string path = "fileupload7/" + hpf.FileName;
                        if (i < 5)
                        {
                            arr[i] = path;
                            path21 = arr[0]; path22 = arr[1]; path23 = arr[2]; path24 = arr[3]; path25 = arr[4];

                        }
                    }
                }
            }
        }

    }


What I have tried:

Above i Post You my Code i am using multiple time file upload i am facing problem
when i choose file for first asp control id-flpProspectus it is working fine
but when i choose file for second asp control id-flpOrientation it is not working properly i am using foreeach loop but not working

so please help me
Posted
Updated 7-Apr-17 19:34pm
v2
Comments
Member 12183079 5-Apr-17 8:00am    
please answer somebody
Karthik_Mahalingam 5-Apr-17 12:50pm    
what does it mean "it is not working properly"
any error ?
why dont you call all the 5 function in the button click event
Bryian Tan 5-Apr-17 13:37pm    
I thought we solved this last time. Is there a new requirement?

How to upload multiple files using multiple fileupload controls in ASP.NET?[^]
Member 12183079 6-Apr-17 1:05am    
Dear sir
i am using 5 file upload control in single button click for store path in database
last time it was working but now i do not know why it is not working i did not getting any error

you know what sir i am getting problem when it call second function FileUpload(2)
then it get in allkey value =hfc.AllKeys[0x00000001]="ctl00$ContentPlaceHolder1$flpOrientation"
but i am getting i=0x00000000 so it is not going inside the loop first time second time it find 0x00000001 then it goes inside loop

that is i am facing problem so kindely plz hel me

1 solution

I think this is the better way to do it. Not sure why your code is displaying 0x000000001 in the hfc.GetKey(i) function, you need to investigate that.
C#
public class FileUploadList
        {
            public  int Position { get; set; }
            public string ControlId { get; set; }
            public string filePath { get; set; }
        }

        string path1, path2, path3, path4, path5, path6, path7, path8, path9, path10, path11, path12, path13, path14, path15,path16, path17, path18, path19, path20, path21, path22, path23, path24, path25 = null;

        /// <summary>
        /// save the file into folder and set the path
        /// </summary>
        /// <param name="hfc"></param>
        /// <param name="lstFile"></param>
        internal void FileUploadHelper(HttpFileCollection hfc, List<FileUploadList> lstFile)
        {
            if (lstFile.Count > 0 )
            {
                string fileExtension = string.Empty; 
                int fileSize = 0;
                int arrPos = 0;
                string[] arr = new string[5];

                foreach (FileUploadList file in lstFile)
                {
                    string filePath = file.filePath;

                    fileExtension = System.IO.Path.GetExtension(hfc[file.Position].FileName);
                    fileSize = hfc[file.Position].ContentLength;
                    if (hfc[file.Position].ContentLength > 0)
                    {
                        hfc[file.Position].SaveAs(Server.MapPath(string.Format("~/{0}/", filePath)) + System.IO.Path.GetFileName(hfc[file.Position].FileName));
                        string filepath = Server.MapPath(string.Format("~/{0}/", filePath));
                        string path = string.Format("{0}/{1}", filePath, System.IO.Path.GetFileName(hfc[file.Position].FileName));

                        arr[arrPos] = path;
                        arrPos++;
                    }
                }

                switch (lstFile.FirstOrDefault().ControlId)
                {
                    case "flpProspectus":
                        path1 = arr[0]; path2 = arr[1]; path3 = arr[2]; path4 = arr[3]; path5 = arr[4];
                        break;
                    case "flpOrientation":
                        path6 = arr[0]; path7 = arr[1]; path8 = arr[2]; path9 = arr[3]; path10 = arr[4];
                        break;
                    case "flpEnquiry":
                        path11 = arr[0]; path12 = arr[1]; path13 = arr[2]; path14 = arr[3]; path15 = arr[4];
                        break;
                    case "flpPunishment":
                        path16 = arr[0]; path17 = arr[1]; path18 = arr[2]; path19 = arr[3]; path20 = arr[4];
                        break;
                    case "flpnooffir":
                        path21 = arr[0]; path22 = arr[1]; path23 = arr[2]; path24 = arr[3]; path25 = arr[4];
                        break;
                }
            }
        }

        /// <summary>
        /// group the control in a List
        /// </summary>
        public void FileUploads()
        {
            if (flpProspectus.HasFile || flpOrientation.HasFile || flpEnquiry.HasFile || flpPunishment.HasFile || flpnooffir.HasFile)
            {
                HttpFileCollection hfc = Request.Files;

                List<FileUploadList> lstFile = new List<FileUploadList>();

                string filePath = string.Empty;

                for (int i = 0; i < hfc.Count; i++)
                {
                    FileUploadList ful = new FileUploadList();

                    if (hfc[i].ContentLength > 0)
                    {
                        if (hfc.GetKey(i).Contains(flpOrientation.ID))
                        {
                            ful.Position = i;
                            ful.ControlId = flpOrientation.ID;
                            ful.filePath = "college/fileupload3";
                        }

                        if (hfc.GetKey(i).Contains(flpProspectus.ID))
                        {
                            ful.Position = i;
                            ful.ControlId = flpProspectus.ID;
                            ful.filePath = "college/fileupload4";
                        }

                        if (hfc.GetKey(i).Contains(flpEnquiry.ID))
                        {
                            ful.Position = i;
                            ful.ControlId = flpEnquiry.ID;
                            ful.filePath = "college/fileupload5";
                        }

                        if (hfc.GetKey(i).Contains(flpPunishment.ID))
                        {
                            ful.Position = i;
                            ful.ControlId = flpPunishment.ID;
                            ful.filePath = "college/fileupload6";
                        }

                        if (hfc.GetKey(i).Contains(flpnooffir.ID))
                        {
                            ful.Position = i;
                            ful.ControlId = flpnooffir.ID;
                            ful.filePath = "college/fileupload7";
                        }

                        lstFile.Add(ful);
                    }
                }

                FileUploadHelper(hfc, lstFile.Where(w => w.ControlId == flpOrientation.ID).ToList());
                FileUploadHelper(hfc, lstFile.Where(w => w.ControlId == flpProspectus.ID).ToList());
                FileUploadHelper(hfc, lstFile.Where(w => w.ControlId == flpEnquiry.ID).ToList());
                FileUploadHelper(hfc, lstFile.Where(w => w.ControlId == flpPunishment.ID).ToList());
                FileUploadHelper(hfc, lstFile.Where(w => w.ControlId == flpnooffir.ID).ToList());

            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            FileUploads();
        }
 
Share this answer
 
v2
Comments
Member 12183079 8-Apr-17 1:33am    
thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900