从FTP上传文件和下载文件 [英] Upload File and Download File from 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
,因为它不工作在 FTP
仅在 HTTP
上。我的假设是否正确?如果不是我可以让它工作吗?
This solution works. But i seen that WebClient
has a method DownloadFile
wich 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?
还有其他解决方案上传/下载 .exe
文件到ftp使用 FtpWebRequest
?
And is there any other solution for uploading/downloading a .exe
file to ftp using FtpWebRequest
?
推荐答案
你需要说您正在上传文本或二进制文件。
请求声明后添加以下行&初始化:
You need to say whether you're uploading text or binary files. Add the following line after request is declared & initialised:
request.UseBinary = true;
http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.usebinary.aspx
这篇关于从FTP上传文件和下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!