问题
I am using asp.net, c#, MVC and nHibernate and I am trying to upload a file from a local machine to the server and replicate the file to the different server. I was able to upload file to the server and copy the file from one folder to the other folder on the same server without any problem.But how can I copy the file from one server to another server. Please follow the link to see how to copy a file from one folder to another folder on the same server. Click to see my answer to the file upload question.[please look for answer by kalyan]
Please help. Thank you.
回答1:
The only way I think this would work is if you FTP to the second server from your first server.
You can use System.Net.FtpWebRequest and System.Net.FtpWebResponse libraries
回答2:
Finally I got it figured out.. here is the sweet code for my own problem.
Side Note:(part I was missing before..) Before you do any thing you should have a FTP site. So, from the IIS (on the server) create a FTP site and point the root directory to the folder that you want to upload or download and manually change the username and password (mine: username: administrator, password: sweet123) from the properties of the site if necessary. (steps are very simple u can easily understand once u start creating an FTP site). I assume that you have your FTP site ready. Now, let us say the url is ftp://10.2.1.111/Images/.
And dont forget to add System.Net and System.IO to your namespace.
now from your code.
string CompleteDPath = "";
CompleteDPath = "ftp://10.2.1.111/Images/";
string UName = "";
string PWD = "";
UName = "administrator";
PWD = "sweet123";
WebRequest reqObj = WebRequest.Create(CompleteDPath + fname);
reqObj.Method = WebRequestMethods.Ftp.UploadFile;
reqObj.Credentials = new NetworkCredential(UName, PWD);
FileStream streamObj = System.IO.File.OpenRead(_FULLlocalpathofthefile + fname);
byte[] buffer = new byte[streamObj.Length + 1];
streamObj.Read(buffer, 0, buffer.Length);
streamObj.Close();
streamObj = null;
reqObj.GetRequestStream().Write(buffer, 0, buffer.Length);
reqObj = null;
来源:https://stackoverflow.com/questions/2463803/copy-files-between-servers-asp-net-mvc