Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C#/.NET中向FTP服务器上载和下载文件_C#_.net_Ftp_Webclient_Ftpwebrequest - Fatal编程技术网

在C#/.NET中向FTP服务器上载和下载文件

在C#/.NET中向FTP服务器上载和下载文件,c#,.net,ftp,webclient,ftpwebrequest,C#,.net,Ftp,Webclient,Ftpwebrequest,我正在使用.NET4c。我正在尝试上载一个ZIP文件,然后将其下载到(我的)服务器 为了上传我有 using (WebClient client = new WebClient()) { FtpWebRequest request = (FtpWebRequest)WebRequest.Create(MyUrl); request.Method = WebRequestMethods.Ftp.UploadFile; request.EnableSsl = false;

我正在使用.NET4c。我正在尝试上载一个ZIP文件,然后将其下载到(我的)服务器

为了上传我有

using (WebClient client = new WebClient())
{
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(MyUrl);
    request.Method = WebRequestMethods.Ftp.UploadFile;
    request.EnableSsl = false;
    request.Credentials = new NetworkCredential(MyLogin, MyPassword);
    byte[] fileContents = null;
    using (StreamReader sourceStream = new StreamReader(LocalFilePath))
    {
        fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
    }
    request.ContentLength = fileContents.Length;
    using (Stream requestStream = request.GetRequestStream())
    {
        requestStream.Write(fileContents, 0, fileContents.Length);
    }
    FtpWebResponse response = null;
    response = (FtpWebResponse)request.GetResponse();
    response.Close();
}
using (WebClient client = new WebClient())
{
    string HtmlResult = String.Empty;
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(remoteFile);
    request.Method = WebRequestMethods.Ftp.DownloadFile;
    request.EnableSsl = false;
    request.Credentials = new NetworkCredential(MyLogin, MyPassword);
    using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
    using (Stream responseStream = response.GetResponseStream())
    using (StreamReader reader = new StreamReader(responseStream))
    using (FileStream writer = new FileStream(localFilename, FileMode.Create))
    {
        long length = response.ContentLength;
        int bufferSize = 2048;
        int readCount;
        byte[] buffer = new byte[2048];
        readCount = responseStream.Read(buffer, 0, bufferSize);
        while (readCount > 0)
        {
            writer.Write(buffer, 0, readCount);
            readCount = responseStream.Read(buffer, 0, bufferSize);
        }
    }
}
这似乎是可行的,因为我在服务器上得到了一个大小合适的文件

1) 我如何流式处理它,而不是首先将其加载到内存中?我将上传非常大的文件

下载我有

using (WebClient client = new WebClient())
{
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(MyUrl);
    request.Method = WebRequestMethods.Ftp.UploadFile;
    request.EnableSsl = false;
    request.Credentials = new NetworkCredential(MyLogin, MyPassword);
    byte[] fileContents = null;
    using (StreamReader sourceStream = new StreamReader(LocalFilePath))
    {
        fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
    }
    request.ContentLength = fileContents.Length;
    using (Stream requestStream = request.GetRequestStream())
    {
        requestStream.Write(fileContents, 0, fileContents.Length);
    }
    FtpWebResponse response = null;
    response = (FtpWebResponse)request.GetResponse();
    response.Close();
}
using (WebClient client = new WebClient())
{
    string HtmlResult = String.Empty;
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(remoteFile);
    request.Method = WebRequestMethods.Ftp.DownloadFile;
    request.EnableSsl = false;
    request.Credentials = new NetworkCredential(MyLogin, MyPassword);
    using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
    using (Stream responseStream = response.GetResponseStream())
    using (StreamReader reader = new StreamReader(responseStream))
    using (FileStream writer = new FileStream(localFilename, FileMode.Create))
    {
        long length = response.ContentLength;
        int bufferSize = 2048;
        int readCount;
        byte[] buffer = new byte[2048];
        readCount = responseStream.Read(buffer, 0, bufferSize);
        while (readCount > 0)
        {
            writer.Write(buffer, 0, readCount);
            readCount = responseStream.Read(buffer, 0, bufferSize);
        }
    }
}
2) 一切似乎都正常。。。除非我试图解压缩下载的ZIP文件,否则我会得到一个无效的ZIP文件。

Upload 使用.NET framework将二进制文件上载到FTP服务器最简单的方法是:

如果您需要更大的控制,而
WebClient
不提供(如ascii/文本传输模式、传输恢复等),请使用。简单的方法是使用以下命令将
文件流
复制到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())
{
    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);
    } 
}
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
),请参阅:

如果要上载文件夹中的所有文件,请参阅


下载 使用.NET framework从FTP服务器下载二进制文件最简单的方法是:

如果您需要更大的控制,而
WebClient
不提供(如ascii/文本传输模式等),请使用。简单的方法是使用以下命令将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.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);
    } 
}
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
),请参阅:

如果要从远程文件夹下载所有文件,请参阅

.

哇,谢谢你这么详尽的解释!它真的帮助了我。