从FTP上传文件和下载文件

Spr*_*dzz 4 c# ftp upload download ftpwebrequest

我正在尝试制作一个上传/下载.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();
Run Code Online (Sandbox Code Playgroud)

此解决方案有效.但我看到WebClient有一种方法DownloadFile没有奏效.我想是因为它不工作FTPHTTP.我的假设是真的吗?如果不是,我怎么能让它工作?

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

Mar*_*ryl 12

双方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");
Run Code Online (Sandbox Code Playgroud)

如果您需要更大的控件WebClient(如TLS/SSL加密,ascii /文本传输模式等),请使用FtpWebRequest.简单的方法是FileStream使用Stream.CopyTo以下方法将a复制到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);
}
Run Code Online (Sandbox Code Playgroud)

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

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);
    } 
}
Run Code Online (Sandbox Code Playgroud)

对于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");
Run Code Online (Sandbox Code Playgroud)

如果您需要更大的控件WebClient(如TLS/SSL加密,ascii /文本传输模式等),请使用FtpWebRequest.简单的方法是将FTP响应流复制到FileStream使用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);
}
Run Code Online (Sandbox Code Playgroud)

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

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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


Joh*_*van 3

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

request.UseBinary = true;
Run Code Online (Sandbox Code Playgroud)

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