Click here to Skip to main content
16,016,527 members
Please Sign up or sign in to vote.
4.43/5 (3 votes)
See more:
I have used AjaxFileUpload to upload multiple image files. Now i want to store the uploaded images inside rootfolder>subfolder.

The rootfolder is in the name of the user.
The rootfolder is created dynamically by taking the session of the user who has logged in
Like this:

string username = Session["username"].ToString();


I am able to create this folder and save images in it. but i want to save it in subfolder.

the subfolder is also created dynamically but this time i have to take the value(id) from the database and name the folder by that id name. (this is so that i can refer to the database)

I know how to create a new folder using Server.MapPath();
Here is the code for it in brief

using System.IO
    if (Directory.Exists(Server.MapPath(uploadPath))) return;
    else Directory.CreateDirectory(Server.MapPath(uploadPath));

where uploadPath is the folder's name i want to create dynamiclly.

but how do I include the subfolder too in my Server.MapPath() so as to make my path as rootfolder/subfolder ?

Small example to make the question understandable.
I am a seller. I have posted 4 ads online.
Now when i am posting my 5th ad i want to include many images.
these images should be saved in the folder

Seller/5/imagename.jpg.
(where Seller is the username(main folder), 5 is the advertID in the database and the name of the subfolder)

How do i do this? Please help.
I am using asp.net c#
Posted
Comments
ZurdoDev 16-Jan-14 10:01am    
What's your question? How do you add a subfolder in Server.MapPath? You just add it on the string so I am not sure what your question is.
Member 9722231 17-Jan-14 1:40am    
My question is how do I name the dynamically created subfolder with a value stored in the database. i.e. i need to create and name it simultaneously and the naming should take a value from the database. How can I do that? Can i do that in Server.MapPath()?
ZurdoDev 17-Jan-14 7:16am    
After your Server.MapPath then just add on the part from the db and created the directory as Solution 1 points out.

1 solution

check this code

C#
string subfolderPath = System.Web.HttpContext.Current.Server.MapPath("~");
                       string subfolderName = "get from data base";
                       subfolderPath += "\\"+subfolderName+"\\";
                       if (!System.IO.Directory.Exists(subfolderPath))
                           System.IO.Directory.CreateDirectory(subfolderPath);
 
Share this answer
 
v2

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