从 FTP 上传文件和下载文件

Upload file and download file from FTP(从 FTP 上传文件和下载文件)
本文介绍了从 FTP 上传文件和下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试制作一个将 .exe 文件上传/下载到 FTP

I am trying to make a program that uploads/downloads .exe file to a FTP

我尝试使用 FtpWebRequest,但我只成功上传和下载 .txt 文件.

I tried using FtpWebRequest, but I only succeed to upload and download .txt files.

然后我在这里找到了使用 WebClient 下载的解决方案:

Then i found here a solution for downloading using the 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.我的假设是真的吗?如果不是,我怎样才能让它工作?

This solution works. But I seen that WebClient has a method DownloadFile which did not worked. I think because it doesn't work on FTP only on HTTP. Is my assumption true? If not how can I get it to work?

还有其他解决方案可以使用 FtpWebRequest.exe 文件上传/下载到 ftp 吗?

And is there any other solution for uploading/downloading a .exe file to ftp using FtpWebRequest?

推荐答案

两者 WebClient.UploadFileWebClient.DownloadFile 适用于 FTP 和二进制文件.

Both WebClient.UploadFile and WebClient.DownloadFile work correctly for FTP as well as binary files.

WebClient client = new WebClient();
client.Credentials = new NetworkCredential("username", "password");
client.UploadFile(
    "ftp://ftp.example.com/remote/path/file.zip", @"C:localpathfile.zip");

如果您需要更好的控制,WebClient 不提供(例如 TLS/SSL 加密,ascii/text 传输模式,传输恢复等),使用 FtpWebRequest.简单的方法是使用 FileStream 复制到 FTP 流rel="nofollow noreferrer">Stream.CopyTo:

If you need a greater control, that WebClient does not offer (like TLS/SSL encryption, ascii/text transfer mode, transfer resuming, etc), use FtpWebRequest. Easy way is to just copy a FileStream to FTP stream using 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:localpathfile.zip"))
using (Stream ftpStream = request.GetRequestStream())
{
    fileStream.CopyTo(ftpStream);
}

如果你需要监控一个上传进度,你必须自己分块复制内容:

If you need to monitor an upload 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.UploadFile;  

using (Stream fileStream = File.OpenRead(@"C:localpathfile.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)见:
如何使用 FtpWebRequest 显示上传进度条

For GUI progress (WinForms ProgressBar), see:
How can we show progress bar for upload with FtpWebRequest

如果要上传文件夹中的所有文件,请参阅
C#递归上传到FTP服务器.

If you want to upload all files from a folder, see
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:localpathfile.zip");

如果您需要更好的控制,WebClient 不提供(例如 TLS/SSL 加密,ascii/text 传输模式,恢复传输等),使用FtpWebRequest.简单的方法是使用 FileStream" rel="nofollow noreferrer">Stream.CopyTo:

If you need a greater control, that WebClient does not offer (like TLS/SSL encryption, ascii/text transfer mode, resuming transfers, etc), use FtpWebRequest. Easy way is to just copy an FTP response stream to FileStream using 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:localpathfile.zip"))
{
    ftpStream.CopyTo(fileStream);
}

如果你需要监控下载进度,你必须自己分块复制内容:

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:localpathfile.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 下载进度条

For GUI progress (WinForms ProgressBar), see:
FtpWebRequest FTP download with ProgressBar

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

If you want to download all files from a remote folder, see
C# Download all files and subdirectories through FTP.

这篇关于从 FTP 上传文件和下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Custom Error Queue Name when using EasyNetQ for RabbitMQ?(使用 EasyNetQ for RabbitMQ 时自定义错误队列名称?)
How to generate password_hash for RabbitMQ Management HTTP API(如何为 RabbitMQ 管理 HTTP API 生成密码哈希)
Rabbitmq Ack or Nack, leaving messages on the queue(Rabbitmq Ack 或 Nack,将消息留在队列中)
Setup RabbitMQ consumer in ASP.NET Core application(在 ASP.NET Core 应用程序中设置 RabbitMQ 消费者)
Specify Publish timeouts in mass transit(指定公共交通中的发布超时)
RabbitMQ asynchronous support(RabbitMQ 异步支持)