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#

2

There are 2 best solutions below

2
On

As far as I know, you can't do one statement to create folder and subfolders because you need the folder to be created first.

You have all of the code you need, you just need to repeat it. Check to see if the main folder (username) exists. If it doesn't create it, if it does, check to see if the subfolder exists. If it doesn't, create it.

Just work through that logic and you'll be set.

0
On

User Path.Combine to add your root and the user id:

   var userPath = Path.Combine(uploadPath,userID) 

This is the safest way to create what you need. The Directory.CreateDirectory method will create all the subfolders as needed:

 var userPath = Path.Combine(uploadPath,userID) 
if (Directory.Exists(Server.MapPath(userPath))) return;
else Directory.CreateDirectory(Server.MapPath(userPath));