Karthik
Karthik

Reputation: 71

How to use webclient to create a directory when uploading a file in asp.net

I am trying with below code, to Upload a document in remote server, but it shows an Error "The given path's format is not supported."

using (WebClient client = new WebClient())
{

                  client.Credentials = new NetworkCredential(UserName, Password);

                  Directory.CreateDirectory(@"https://Sample.com/Folders/Test Folder/");     //Error shown here

                  client.UploadFile("https://Sample.com/Folders/Test Folder/", "POST" , @"C:\\Sample Document.docx");                

  }

How can I create the necessary directories with webclient ?

Upvotes: 0

Views: 2001

Answers (1)

Chandan Kumar
Chandan Kumar

Reputation: 4638

You could try like below

//File Path
string filepath = System.Web.HttpContext.Current.Server.MapPath("~/Folders/Test Folder/");  
if (!Directory.Exists(filepath))
{
     Directory.CreateDirectory(filepath);
}

Upvotes: 2

Related Questions