从 FTP 上传文件和下载文件
Posted
技术标签:
【中文标题】从 FTP 上传文件和下载文件【英文标题】:Upload file and download file from FTP 【发布时间】:2012-10-18 01:41:45 【问题描述】:我正在尝试制作一个将.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 吗?
【问题讨论】:
【参考方案1】:您需要说明您上传的是文本文件还是二进制文件。 在声明和初始化请求后添加以下行:
request.UseBinary = true;
http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.usebinary.aspx
【讨论】:
谢谢,它有效。也欢迎任何有其他解决方案的人分享。也有人成功地使用了 WebClient , DownloadFile 方法的 ftp 吗? 我没有使用过这些,但发现了一些可能有用的链接:dijksterhuis.org/webclient-class-upload-download-ftp-files & codeproject.com/Articles/17202/… 但是true
是默认值,所以我看不出这有什么帮助。
@MartinPrikryl 好点:docs.microsoft.com/en-gb/dotnet/api/…【参考方案2】:
上传很容易...
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 ^
【讨论】:
【参考方案3】:WebClient.UploadFile
和 WebClient.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 encryption、ascii/文本传输模式、传输恢复等),请使用FtpWebRequest
。简单的方法是使用Stream.CopyTo
将FileStream
复制到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
),请参阅:How can we show progress bar for upload with FtpWebRequest
如果您想上传文件夹中的所有文件,请参阅Recursive upload to FTP server in C#。
下载
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 encryption、ascii/文本传输模式、resuming transfers 等),请使用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
),请参阅:FtpWebRequest FTP download with ProgressBar
如果您想从远程文件夹下载所有文件,请参阅C# Download all files and subdirectories through FTP。
【讨论】:
以上是关于从 FTP 上传文件和下载文件的主要内容,如果未能解决你的问题,请参考以下文章