Click here to Skip to main content
14,635,966 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a webpage(.aspx) which contains MULTIPLE FileUpload controls.

<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
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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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
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];
            }
        }
    }
}
   
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
Bryian Tan 26-Mar-17 13:27pm
   
I have updated the solution, take a look.
Member 12183079 27-Mar-17 2:40am
   
Thank You
Member 12183079 6-Apr-17 2:22am
   
sir above is your code working but i getting path problem means i mention on 1 file upload control user can choose only 5 file and 2 nd file upload control same 5 file so suppose user use in 1 file upload control only 2 file
so i want to store file path only in path1 and path2 and path3 path4 ,path 5 should null value and user choose on second file upload control 3 file then file should be strored in file path6 ,path7 and path8 like that
so please help me

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100