C# 使用 FtpWebRequest 下载文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12519290/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Downloading files Using FtpWebRequest
提问by Rick Eyre
I'm trying to download a file using FtpWebRequest.
我正在尝试使用FtpWebRequest.
private void DownloadFile(string userName, string password, string ftpSourceFilePath, string localDestinationFilePath)
{
int bytesRead = 0;
byte[] buffer = new byte[1024];
FtpWebRequest request = CreateFtpWebRequest(ftpSourceFilePath, userName, password, true);
request.Method = WebRequestMethods.Ftp.DownloadFile;
Stream reader = request.GetResponse().GetResponseStream();
BinaryWriter writer = new BinaryWriter(File.Open(localDestinationFilePath, FileMode.CreateNew));
while (true)
{
bytesRead = reader.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
break;
writer.Write(buffer, 0, bytesRead);
}
}
It uses this CreateFtpWebRequestmethod I created:
它使用CreateFtpWebRequest我创建的这种方法:
private FtpWebRequest CreateFtpWebRequest(string ftpDirectoryPath, string userName, string password, bool keepAlive = false)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(ftpDirectoryPath));
//Set proxy to null. Under current configuration if this option is not set then the proxy that is used will get an html response from the web content gateway (firewall monitoring system)
request.Proxy = null;
request.UsePassive = true;
request.UseBinary = true;
request.KeepAlive = keepAlive;
request.Credentials = new NetworkCredential(userName, password);
return request;
}
It downloads it. But the information is always corrupted. Anyone know what's going on?
它下载它。但是信息总是被破坏。有谁知道这是怎么回事?
采纳答案by Rick Eyre
Just figured it out:
刚刚想通了:
private void DownloadFile(string userName, string password, string ftpSourceFilePath, string localDestinationFilePath)
{
int bytesRead = 0;
byte[] buffer = new byte[2048];
FtpWebRequest request = CreateFtpWebRequest(ftpSourceFilePath, userName, password, true);
request.Method = WebRequestMethods.Ftp.DownloadFile;
Stream reader = request.GetResponse().GetResponseStream();
FileStream fileStream = new FileStream(localDestinationFilePath, FileMode.Create);
while (true)
{
bytesRead = reader.Read(buffer, 0, buffer.Length);
if (bytesRead == 0)
break;
fileStream.Write(buffer, 0, bytesRead);
}
fileStream.Close();
}
Had to use a FileStream instead.
不得不改用 FileStream。
回答by Martin Prikryl
Easiest way
最简单的方法
The most trivial way to download a file from an FTP server using .NET framework is using WebClient.DownloadFilemethod:
使用 .NET 框架从 FTP 服务器下载文件的最简单方法是使用WebClient.DownloadFile方法:
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");
Advanced options
高级选项
Use FtpWebRequestclass, if you need a greater control only, that WebClientclass does not offer (like TLS/SSL encryption, progress monitoring etc). An easy way, is to just copy an FTP response stream to FileStreamusing Stream.CopyTomethod:
使用FtpWebRequestclass,如果您只需要更好的控制,则WebClient该类不提供(如TLS/SSL 加密、进度监控等)。一种简单的方法是将 FTP 响应流复制到FileStreamusingStream.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);
}
Progress monitoring
进度监控
If you need to monitor a download progress, you have to copy the contents by chunks yourself:
如果您需要监控下载进度,您必须自己分块复制内容:
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);
}
}
For GUI progress (WinForms ProgressBar), see:
FtpWebRequest FTP download with ProgressBar
有关 GUI 进度 (WinForms ProgressBar),请参阅:
FtpWebRequest FTP download with ProgressBar
Downloading folder
下载文件夹
If you want to download all files from a remote folder, see
C# Download all files and subdirectories through FTP.
如果要从远程文件夹
下载所有文件,请参阅C# 通过 FTP 下载所有文件和子目录。

