Click here to Skip to main content
15,893,904 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have a webpage(.aspx) which contains MULTIPLE FileUpload controls.

ASP.NET
<asp:FileUpload ID="flpProspectus"  AllowMultiple="true" class="form-control" runat="server"  Style="width: 40%; margin-top: -12px;" />
<asp:FileUpload ID="flpOrientation"  AllowMultiple="true" class="form-control" runat="server"  Style="width: 40%; margin-top: -12px;" />


Default.aspx.cs
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++)
            {
                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 path = "college/fileupload3/" + hpf.FileName;
                    if (i < 5)
                    {
                        arr[i] = path;
                        path1 = arr[0]; path2 = arr[1]; path3 = arr[2]; path4 = arr[3]; path5 = arr[4];

                    }

                }
            }
        }
    }


What I have tried:

Here, Request.Files will get collectively all files from both the FileUploadControls.

I am NOT able to IDENTIFY which file(s) are from specific FileUpload control?

I know its possible in 4.5 but my current framework is 4.0 and i don't want to upgrade to 4.5. Any solution using existing 4.0 framework??

Help appreciated!

Please note: This is not DUPLICATE question as my requirement is to upload and identify the files of different FileUpload controls on single page.
Posted
Updated 25-Mar-17 10:32am
v2

1 solution

Why not use the PostedFiles property? You might need to rewrite the "if (i < 5)...", not clear what you trying to do there.

Updated solution to target Framework 4.0
C#
if (flpProspectus.HasFile || flpOrientation.HasFile)
{
    string fileExtension = string.Empty; // System.IO.Path.GetExtension(flpProspectus.FileName);
    int fileSize = 0;// flpProspectus.PostedFile.ContentLength;
    HttpFileCollection hfc = Request.Files;
    string[] arr = new string[5];
    string path1, path2, path3, path4, path5 = string.Empty;
    string filePath = string.Empty;

    for (int i = 0; i < hfc.Count; i++)
    {
        if (hfc.GetKey(i) == "flpOrientation")
        {
            filePath = "college/fileupload3";
        }

        if (hfc.GetKey(i) == "flpProspectus")
        {
            filePath = "college/fileupload4";
        }

        fileExtension = System.IO.Path.GetExtension(hfc[i].FileName);
        fileSize = hfc[i].ContentLength;
        if (hfc[i].ContentLength > 0)
        {
            hfc[i].SaveAs(Server.MapPath(string.Format("~/{0}/", filePath)) + System.IO.Path.GetFileName(hfc[i].FileName));
            string filepath = Server.MapPath(string.Format("~/{0}/", filePath));
            string path = string.Format("{0}/", filePath) + hfc[i].FileName;
            if (i < 5)
            {
                arr[i] = path;
                path1 = arr[0]; path2 = arr[1]; path3 = arr[2]; path4 = arr[3]; path5 = arr[4];
            }
        }
    }
}
 
Share this answer
 
v3
Comments
Member 12183079 25-Mar-17 23:20pm    
Dear sir
when i try this code then i get error in PostedFiles and i want to strore path in array index because i want to pass in column where i made in table so kinley help me
Bryian Tan 25-Mar-17 23:29pm    
What is the error message?
Member 12183079 25-Mar-17 23:49pm    
system.web.ui.webcontrols.fileupload' does not contain a definition for 'postedfile'
Bryian Tan 26-Mar-17 0:35am    
refer to the updated solution.
Member 12183079 26-Mar-17 12:56pm    
sorry i did not get it

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