从FTP上传文件和下载文件

我正在尝试制作一个上传/下载.exe文件到FTP

我尝试使用FtpWebRequest ,但我只能成功上传和下载.txt文件。

然后我在这里找到了一个使用WebClient下载的解决方案:

 WebClient request = new WebClient(); request.Credentials = new NetworkCredential("username", "password"); byte[] fileData = request.DownloadData("ftp://myFTP.net/"); FileStream file = File.Create(destinatie); file.Write(fileData, 0, fileData.Length); file.Close(); 

该解决方案有效。 但我看到WebClient有一个方法DownloadFile没有用。 我认为因为它仅在HTTP上不适用于FTP 。 我的假设是真的吗? 如果没有,我怎么能让它工作?

有没有其他解决方案使用FtpWebRequest上传/下载.exe文件到ftp?

WebClient.UploadFileWebClient.DownloadFile可以正常用于FTP以及二进制文件。

上传

 WebClient client = new WebClient(); client.Credentials = new NetworkCredential("username", "password"); client.UploadFile( "ftp://ftp.example.com/remote/path/file.zip", @"C:\local\path\file.zip"); 

如果您需要更大的控件, WebClient不提供(如TLS / SSL加密,ascii /文本传输模式等),请使用FtpWebRequest 。 简单的方法是使用Stream.CopyToFileStream复制到FTP流:

 FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip"); request.Credentials = new NetworkCredential("username", "password"); request.Method = WebRequestMethods.Ftp.UploadFile; using (Stream fileStream = File.OpenRead(@"C:\local\path\file.zip")) using (Stream ftpStream = request.GetRequestStream()) { fileStream.CopyTo(ftpStream); } 

如果您需要监控上传进度,则必须自己按块复制内容:

 FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip"); request.Credentials = new NetworkCredential("username", "password"); request.Method = WebRequestMethods.Ftp.UploadFile; using (Stream fileStream = File.OpenRead(@"C:\local\path\file.zip")) using (Stream ftpStream = request.GetRequestStream()) { byte[] buffer = new byte[10240]; int read; while ((read = fileStream.Read(buffer, 0, buffer.Length)) > 0) { ftpStream.Write(buffer, 0, read); Console.WriteLine("Uploaded {0} bytes", fileStream.Position); } } 

对于GUI进度(WinForms ProgressBar ),请参阅:
我们如何使用FtpWebRequest显示上传进度条

如果要上载文件夹中的所有文件,请参阅
使用WebClient上载文件目录 。


下载

 WebClient client = new WebClient(); client.Credentials = new NetworkCredential("username", "password"); client.DownloadFile( "ftp://ftp.example.com/remote/path/file.zip", @"C:\local\path\file.zip"); 

如果您需要更大的控件, WebClient不提供(如TLS / SSL加密,ascii /文本传输模式等),请使用FtpWebRequest 。 简单的方法是使用Stream.CopyTo将FTP响应流复制到FileStream

 FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip"); request.Credentials = new NetworkCredential("username", "password"); request.Method = WebRequestMethods.Ftp.DownloadFile; using (Stream ftpStream = request.GetResponse().GetResponseStream()) using (Stream fileStream = File.Create(@"C:\local\path\file.zip")) { ftpStream.CopyTo(fileStream); } 

如果需要监视下载进度,则必须自己按块复制内容:

 FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://ftp.example.com/remote/path/file.zip"); request.Credentials = new NetworkCredential("username", "password"); request.Method = WebRequestMethods.Ftp.DownloadFile; using (Stream ftpStream = request.GetResponse().GetResponseStream()) using (Stream fileStream = File.Create(@"C:\local\path\file.zip")) { byte[] buffer = new byte[10240]; int read; while ((read = ftpStream.Read(buffer, 0, buffer.Length)) > 0) { fileStream.Write(buffer, 0, read); Console.WriteLine("Downloaded {0} bytes", fileStream.Position); } } 

对于GUI进度(WinForms ProgressBar ),请参阅:
使用ProgressBar进行FtpWebRequest FTP下载

如果要从远程文件夹下载所有文件,请参阅
C#通过FTP下载所有文件和子目录 。

你需要说明你是上传文本还是二进制文件。 在声明和初始化请求后添加以下行:

 request.UseBinary = true; 

http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.usebinary.aspx

上传很容易……

 void Upload(){ Web client = new WebClient(); client.Credentials = new NetworkCredential(username, password); client.BaseAddress = "ftp://ftpsample.net/"; client.UploadFile("sample.txt", "sample.txt"); //since the baseaddress } 

下载也很简单…我在下面创建的代码使用BackgroundWorker (因此下载开始时不会冻结接口/应用程序)。 另外,我使用lambda (=>)缩短了代码。

 void Download(){ Web client = new WebClient(); client.Credentials = new NetworkCredential(username, password); BackgroundWorker bg = new BackgroundWorker(); bg.DoWork += (s, e) => { client.DownloadFile("ftp://ftpsample.net/sample.zip", "sample.zip"); }; bg.RunWorkerCompleted += (s, e) => { //when download is completed MessageBox.Show("done downloading"); download1.Enabled = true; //enable download button progressBar.Value = 0; // reset progressBar }; bg.RunWorkerAsync(); download1.Enabled = false; //disable download button while (bg.IsBusy) { progressBar.Increment(1); //well just for extra/loading Application.DoEvents(); //processes all windows messages currently in the message queue } } 

您还可以使用DownloadFileAsync方法显示实际进度文件下载:

 client.DownloadFileAsync(new Uri("ftp://ftpsample.net/sample.zip"), "sample.zip"); client.DownloadProgressChanged += (s, e) => { progressBar.Value = e.ProgressPercentage; //show real download progress }; client.DownloadFileCompleted += (s, e) => { progressBar.Visible = false; // other codes after the download }; //You can remove the 'while' statement and use this ^