Click here to Skip to main content
14,773,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

How to create folder on any drive dynamically and then upload files to any selected folder.

I tried the code but it creates in the Application folder.

What I have tried:

Controller:
[HttpPost]
       public ActionResult CreateFolder(string folderName)
       {
           string folder = Server.MapPath(string.Format("~/{0}/", folderName));
           if(!Directory.Exists(folder))
           {
               Directory.CreateDirectory(folder);
               ViewBag.Message = "Folder " + folderName.ToString() + " created successfully!";
           }
           else
           {
               ViewBag.Message = "Folder " + folderName.ToString() + "  already exists!";
           }
           return View();
       }


View:
@using (Html.BeginForm("CreateFolder", "Home", FormMethod.Post))
{
    <label for="file">Folder Name:</label>
    <input type="text" name="folderName" />

    <input type="submit" />
   
}
<hr />
@ViewBag.Message
Posted
Updated 25-Jul-20 11:02am
Comments
DerekT-P 25-Jul-20 15:30pm
   
You say "on any drive" but you're using Server.MapPath to get a path relative to your web application. If you want the folder in a specific, absolute place - then put it there. (E.g. Foldername = "d:\mytopfolder\mysubfolder";Directory.CreateDirectory(Foldername); )
Sandeep Mewara 25-Jul-20 15:33pm
   
Don't see where are you taking input for folder path here. Your code is to create folder in the application folder (relative to your application - using Server.MapPath).

Take the path from the user and then use that absolute path to create directory and use.

1 solution

For a web applicaiton, creating a folder using server code will create it on webserver. Don't think you want user to select a path on server to create a folder.

That would leave you with option of a defined location by you on server where you want to create folder. Now, this folder can be user specific.
Roughly, this translates to:
1. Choose/Define a path on server where all the folders will be created. Use that absolute path or relative to your application.
2. Create folder based on user (username or userid)
3. upload data for that user in their folder.

Now, assume:
1. you pointed a web site application (http://www.example.com/) to C:\Inetpub\wwwroot
2. Installed your testuser application (webapp as virtual directory in IIS, marked as application) at: L:\MyWebApps\testuser
3. You choose to upload all files at location: L:\MyWebApps\testuser\alluseruploads

Then code looks like:
string uploadRoot = Server.MapPath("/testuser/alluseruploads");  //returns L:\MyWebApps\testuser\alluseruploads
string folder = string.Format(uploadRoot +"/{0}/", folderName);
           if(!Directory.Exists(folder))
           {
               Directory.CreateDirectory(folder);
               ViewBag.Message = "Folder " + folderName.ToString() + " created successfully!";
           }
   

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