我正在尝试制作一个将 .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.UploadFile
和 WebClient.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 上传文件和下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!