Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/24.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# 使用FTP下载文件无效_C#_Ftp - Fatal编程技术网

C# 使用FTP下载文件无效

C# 使用FTP下载文件无效,c#,ftp,C#,Ftp,我正在尝试使用以下代码从FTP服务器下载文件: public byte[] DownloadFile(string remoteFile) { try { ftpRequest = (FtpWebRequest)FtpWebRequest.Create(ip + '/' + remoteFile); ftpRequest.Credentials = new NetworkCredential(

我正在尝试使用以下代码从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%的速度停止发送数据 这是我的代码中的问题还是什么问题?

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;
        }
    }

我相信问题在于你没有阅读所有的数据。尝试从流中读取,不管它告诉您它有多少字节,直到到达流的末尾

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");
});
Action downloadBrowser=null;
downloadBrowser=新操作((tempDir,文件)=>{
WriteLine(string.Format(“下载文件”{0}',文件));
const int bufferLength=1024;
变量megaByteSize=(缓冲区长度*500);
var dlRequest=(FtpWebRequest)WebRequest.Create(FTP_站点+文件);
dlRequest.Method=WebRequestMethods.Ftp.DownloadFile;
dlRequest.UseBinary=true;
使用(var dlResponse=dlRequest.GetResponse())
{
var dlResponseStream=dlResponse.GetResponseStream();
var fileName=Path.Combine(tempDir,file);
使用(var fs=newfilestream(文件名,FileMode.Create))
{               
var buffer=新字节[bufferLength];
var progressLimit=兆字节化;
var bytesRead=0;
做{
bytesRead=dlResponseStream.Read(缓冲区,0,缓冲区长度);
fs.Write(缓冲区,0,字节读取);
如果(fs.Length>progressLimit){
控制台。写(“*”);
progressLimit+=兆字节化;
}
}而(字节读取>0);
}
}
Console.WriteLine(“\n下载完成”);
});

您是否遇到异常?使用
ftpStream.CopyTo(ms)
其中
ms
MemoryStream
的一个实例,另请参见:。然后,您可以
返回ms.ToArray()获取字节缓冲。您应该学习流的基本知识。流不必返回您请求的所有数据量,因此可能需要多次读取。