c# - 从 FTP 上传文件和下载文件

标签 c# upload ftp 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();

这个解决方案有效。但是我看到 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 encryption、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),请参阅:
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 .

关于c# - 从 FTP 上传文件和下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13109823/

相关文章:

c# - 已进行选择时卡住列表框项目的选择

c# - 为什么 WinForms 标签不想透明的原因?

ios - dSYM 未通过 CLI 命令上传

python - Flask:通过post获取图像

javascript - 使用 formData() 上传多个文件

PHP 颠覆设置 FTP

python - 通过 FTP 确定列表是 Python 中的目录还是文件

c# - 如果匿名类型应该是不可变的,为什么可以更改它?

phpstorm 和远程服务器

c# - 如何使用事件更新类中的 TextBox?