使用FTP下载文件不起作用

时间:2017-09-11 14:18:36

标签: c# ftp

我正在尝试使用以下代码从我的FTP服务器下载文件:

        public byte[] DownloadFile(string remoteFile)
    {
        try
        {
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(ip + '/' + remoteFile);
            ftpRequest.Credentials = new NetworkCredential(user, pass);
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
            ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            ftpStream = ftpResponse.GetResponseStream();
            byte[] byteBuffer = new byte[Convert.ToInt32(getFileSize(remoteFile))];
            int bytesRead = byteBuffer.Length;
            ftpStream.Read(byteBuffer, 0, byteBuffer.Length);
            ftpStream.Close();
            ftpResponse.Close();
            ftpRequest = null;                
            Console.WriteLine("Successful");
            return byteBuffer;
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex);
            byte[] bytes = new byte[0];
            return bytes;
        }

但始终,FileZilla服务器停止以87.7%的速度发送数据 这是我的代码中的问题还是问题是什么?

2 个答案:

答案 0 :(得分:0)

Rene找到了答案:

public byte[] DownloadFile(string remoteFile)
    {
        try
        {
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(ip + '/' + remoteFile);
            ftpRequest.Credentials = new NetworkCredential(user, pass);
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
            ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            ftpStream = ftpResponse.GetResponseStream();
            //byte[] byteBuffer = new byte[Convert.ToInt32(getFileSize(remoteFile))];
            MemoryStream ms = new MemoryStream();
            ftpStream.CopyTo(ms);
            ftpStream.Close();
            ftpResponse.Close();
            ftpRequest = null;                
            Console.WriteLine("Successful");
            return ms.ToArray();
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex);
            byte[] bytes = new byte[0];
            return bytes;
        }
    }

答案 1 :(得分:0)

我认为问题在于你没有阅读所有数据。尝试从流中读取,无论它告诉你它有多少字节,直到你到达流的末尾。

Action<string, string> downloadBrowser = null;
downloadBrowser = new Action<string, string>((tempDir, file) => {
    Console.WriteLine(string.Format("Downloading file '{0}'", file));

    const int bufferLength = 1024;
    var megaByteSize = (bufferLength * 500);

    var dlRequest = (FtpWebRequest) WebRequest.Create(FTP_SITE + file);
    dlRequest.Method = WebRequestMethods.Ftp.DownloadFile;
    dlRequest.UseBinary = true;

    using (var dlResponse = dlRequest.GetResponse())
    {
        var dlResponseStream = dlResponse.GetResponseStream();
        var fileName = Path.Combine(tempDir, file);
        using (var fs = new FileStream(fileName, FileMode.Create))
        {               
            var buffer = new byte[bufferLength];
            var progressLimit = megaByteSize;
            var bytesRead = 0;
            do {
                bytesRead = dlResponseStream.Read(buffer, 0, bufferLength);
                fs.Write(buffer, 0, bytesRead);
                if (fs.Length > progressLimit) {
                    Console.Write("*");
                    progressLimit += megaByteSize;
                }
            } while (bytesRead > 0);
        }
    }

    Console.WriteLine("\nDownload Complete");
});