[C#] 上傳/下載至FTP (使用WebClient)

  • 6425
  • 0
  • 2018-03-31

上傳本機資料至FTP

方法1:

System.Net.WebClient webClient = new System.Net.WebClient();
string sourceFilePath = @"D:\MyDocuments\DataFile.xml";
string webAddress = "http://www.YourDomainName.com/ClientFiles/";
string destinationFilePath= webAddress + "DataFile.xml";
webClient.Credentials = new System.Net.NetworkCredential("username", "password", "domain");
//註:webClient.UploadFile(destinationFilePath, "PUT", sourceFilePath);
//這行可以直接給destinationFilePath跟sourceFilePath即可
webClient.UploadFile(destinationFilePath, "PUT", sourceFilePath);
webClient.Dispose();

 

方法2:

using (WebClient client = new WebClient())
{
    client.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
    //frp走STOR
    //http走post/get
    //經過測試後不加也可以正常使用
    client.UploadFile("ftp://ftpserver.com/target.zip", "STOR", localFilePath);
}

 

Reference:

1.How to upload/download file to/from server using WebClient method…?

http://chiragvidani.blogspot.tw/2011/10/how-to-uploaddownload-file-tofrom.html

2.Upload file to FTP using C#

https://stackoverflow.com/questions/15268760/upload-file-to-ftp-using-c-sharp

3.Upload file and download file from FTP

https://stackoverflow.com/questions/13109823/upload-file-and-download-file-from-ftp?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa