Both WebClient.UploadFile https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient.uploadfile and WebClient.DownloadFile https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient.downloadfile对于 FTP 和二进制文件都可以正常工作。
Upload
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 加密 https://stackoverflow.com/q/4331665/850848#56888555、ascii/文本传输模式、传输恢复等),使用FtpWebRequest https://learn.microsoft.com/en-us/dotnet/api/system.net.ftpwebrequest。简单的方法就是复制一个FileStream
使用 FTP 流Stream.CopyTo https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.copyto:
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 进度(WinFormsProgressBar
), see:
我们如何使用 FtpWebRequest 显示上传进度条 https://stackoverflow.com/q/6341024/850848#45931971
如果您想上传文件夹中的所有文件,请参阅
C# 递归上传到FTP服务器 https://stackoverflow.com/q/60925131/850848.
Download
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 加密 https://stackoverflow.com/q/4331665/850848#56888555, ascii/文本传输模式,恢复传输 https://stackoverflow.com/q/6331133/850848#66154869等),使用FtpWebRequest https://learn.microsoft.com/en-us/dotnet/api/system.net.ftpwebrequest。简单的方法是将 FTP 响应流复制到FileStream
using Stream.CopyTo https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.copyto:
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 进度(WinFormsProgressBar
), see:
FtpWebRequest 使用进度条进行 FTP 下载 https://stackoverflow.com/q/45269263/850848
如果您想从远程文件夹下载所有文件,请参阅
C# 通过FTP下载所有文件及子目录 https://stackoverflow.com/q/37038676/850848.