上传文件,并从FTP下载文件(Upload file and download file from

2019-06-18 16:24发布

我试图做一个程序,上传/下载.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这并没有奏效。 我想是因为它不工作的FTP只有HTTP 。 是我的假设是真的吗? 我如果不怎样才能得到它的工作?

而且是有上传/下载任何其他解决方案.exe文件中使用到ftp FtpWebRequest

Answer 1:

无论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加密,ASCII /文本传送模式等),使用FtpWebRequest 。 简单的方法是只复制FileStream使用到FTP流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:\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 ),请参阅:
我们怎样才能显示上传用的FtpWebRequest进度条

如果你想将所有文件从一个文件夹上传,见
上传使用Web客户端文件的目录 。


下载

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加密,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);
}

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

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下载与进度

如果你想从一个远程文件夹下载的所有文件,请参阅
C#下载通过FTP中的所有文件和子目录 。



Answer 2:

你需要说无论你上传文本或二进制文件。 添加以下行请求宣告与初始化后:

request.UseBinary = true;

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



Answer 3:

上传是很容易...

void Upload(){
    Web client = new WebClient();
    client.Credentials = new NetworkCredential(username, password);
    client.BaseAddress = "ftp://ftpsample.net/";

    client.UploadFile("sample.txt", "sample.txt"); //since the baseaddress
}


下载是一件容易的事......我下面进行的代码使用BackgroundWorker的 (所以开始下载时不会冻结接口/应用)。 另外,我缩短了代码与使用的拉姆达 (=>)

void Download(){
    Web client = new WebClient();
    client.Credentials = new NetworkCredential(username, password);

    BackgroundWorker bg = new BackgroundWorker();
    bg.DoWork += (s, e) => { 
        client.DownloadFile("ftp://ftpsample.net/sample.zip", "sample.zip"); 
    };
    bg.RunWorkerCompleted += (s, e) => { //when download is completed
        MessageBox.Show("done downloading");
        download1.Enabled = true; //enable download button
        progressBar.Value = 0; // reset progressBar
    };
    bg.RunWorkerAsync();
    download1.Enabled = false; //disable download button 
    while (bg.IsBusy)
    {
        progressBar.Increment(1); //well just for extra/loading
        Application.DoEvents(); //processes all windows messages currently in the message queue
    }
}

您还可以使用DownloadFileAsync方法来显示真正的进步文件下载:

client.DownloadFileAsync(new Uri("ftp://ftpsample.net/sample.zip"), "sample.zip");
client.DownloadProgressChanged += (s, e) =>
{
    progressBar.Value = e.ProgressPercentage; //show real download progress
};
client.DownloadFileCompleted += (s, e) =>
{
    progressBar.Visible = false;
    // other codes after the download
};
//You can remove the 'while' statement and use this ^


文章来源: Upload file and download file from FTP