Click here to Skip to main content
15,901,122 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have been able to use the following method Upload multiple files in asp.net[^]to upload files successfully, but i haven't been able to figure out how to write file(s) name and path to sql. I have tried getting file names but it only returns with last file selected and not all files.


C#
string filesname = System.IO.Path.GetFileName(hpf1.FileName);



I have not be able to figure out how to get all files selected names.

Please help.

Per Req:

C#
try
{
    HttpFileCollection hfc1 = Request.Files;
    for (int i = 0; i < hfc1.Count; i++)
    {
        string ponoref = txtRequsitionNo.Text;

        String pathref = Server.MapPath("~/uploads/" + ponoref + "/");

        if (!System.IO.Directory.Exists(pathref))
        {
            System.IO.Directory.CreateDirectory(pathref);
        }

        HttpPostedFile hpf1 = hfc1[i];
        if (hpf1.ContentLength > 0)
        {
            hpf1.SaveAs(Server.MapPath("~/uploads/") + ponoref + @"/" + System.IO.Path.GetFileName(hpf1.FileName));
        }
        string filesname = System.IO.Path.GetFileName(hpf1.FileName);

        string POREF = @"uploads\" + ponoref + @"\" + filesname;

        powholeref.Text = POREF.ToString();
    }
}
catch (Exception)
{

    throw;
}

txtRequsitionNo.Text = string.Empty;
Posted
Updated 15-Dec-15 9:13am
v2
Comments
Tomas Takac 15-Dec-15 11:00am    
Please show the whole code where you extract the file names and save them to database.
r00n3y 15-Dec-15 11:32am    
I have posted the code i use to extract. nothing else. Basically im trying to get all the file names not just last one.
Richard Deeming 15-Dec-15 13:10pm    
You haven't posted anywhere near enough code for anyone to diagnose the problem. You've shown a single line of code which extracts the file name from a variable which is presumably an HttpPostedFile. You haven't shown how you're calling that code, or how it "returns" the last file selected.

Click "Improve question" and update your question with the relevant parts of your code.
r00n3y 15-Dec-15 15:13pm    
Updated the question.
Tomas Takac 16-Dec-15 3:40am    
Well you said your problem is in storing the path and file name to a database. Yet you are not showing the related code, where you are actually storing it. How are we supposed to help you?

You are assigning all the file names into one variable. As you go over the files in the for-loop this is always overwritten by the current file name and naturally after you exit the loop there is the last one. What you need is a list.
C#
var listOfFiles = new List<string>(hfc1.Count);
for (int i = 0; i < hfc1.Count; i++)
{
    // ...

    string filesname = System.IO.Path.GetFileName(hpf1.FileName);
    listOfFiles.Add(filesname);

    // ...
}

// do something with listOfFiles
 
Share this answer
 
Comments
r00n3y 16-Dec-15 10:32am    
Thank you very much, i apologize for not explaining my situation clearly.
Don't post this under Quick Answers - if you got the code from an article, then there is a "Add a Comment or Question" button at the bottom of that article, which causes an email to be sent to the author. They are then alerted that you wish to speak to them.
Posting this here relies on them "dropping by" and realising it is for them.
 
Share this answer
 
Comments
r00n3y 15-Dec-15 11:34am    
I get it, but based on the comments that already there i doubt author will be replying. Thats why i decided to post this under Quick Answers.

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