Click here to Skip to main content
15,888,315 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have read local path from textbox ex. D:\images and uploaded to server by using server.mappath its working fine on localhost

but while code published to server the source path D:\images considered from server path not from client machine .

and I want to read this path from client machine to upload all images from folder to server.

What I have tried:

string folderPath = System.Configuration.ConfigurationManager.AppSettings["UploadPath"].ToString();

  DirectoryInfo d = new DirectoryInfo(@MD.Path);//here I get D:\images 
   FileInfo[] Files = ((d.GetFiles("*.jpg", SearchOption.AllDirectories)).Union(d.GetFiles("*.png", SearchOption.AllDirectories))).ToArray(); //here i get all images 
            string str = "";
            ImageBL objBL = new ImageBL();
            foreach (FileInfo file in Files)
            {

 var InputFileName = Path.GetFileName(file.Name);
  string path = Server.MapPath("~/" + folderPath + "/");
  var RPaths = Server.MapPath("~/" + folderPath + "//" + "Images" + "//" + MD.YrID + "//" + MD.RPath);
 var ServerSavePath = Server.MapPath("~/" + folderPath + "//" + "Images" + "//" + MD.YrID + "//" + MD.RPath + "//" + InputFileName);


<pre>  if (!System.IO.Directory.Exists(RPaths))
                    {
                        System.IO.Directory.CreateDirectory(RPaths);
                    }

                    
                    if (System.IO.File.Exists(ServerSavePath))
                    {
                        ServerSavePath.Replace(ServerSavePath, System.DateTime.Now.ToLocalTime() + "" + ServerSavePath);
                    }
                    else
                    {
                        file.CopyTo(ServerSavePath);
                    }
}
Posted
Updated 24-Apr-17 21:12pm
v2

1 solution

You can't do it.
It appears to work in development because the client and the server are both the same machine - but C# code always runs on the server, never on the client, so all accesses to the "user HDD" in your code are actually accesses to the Server HDD - since they are the same physical machine it looks like it works fine, but as soon as you release to production the client is a thousand miles away and the server has no access to his HDD.

The only way to upload files from the client to the server is to use an upload control of some form and get the user to specify exactly which files he wants to upload to you.
 
Share this answer
 
Comments
Nitin H. 26-Apr-17 2:31am    
Thank you very much!! I have done code with upload control.
OriginalGriff 26-Apr-17 2:36am    
You're welcome!

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