FTP下载不在文件中写入数据

时间:2012-12-05 16:59:18

标签: c# ftp

我编写了一个程序,它从远程ftp站点下载文件并将其保存到本地c盘,然后将该文件上传到单独的服务器。现在的问题是,当文件被下载时,它在本地C上创建的文本文件中没有数据,我无法弄清楚为什么会这样。这是我正在使用的代码

// Download File
    public void download(string remoteFile, string localFile)
    {

        try
        {
            // Create an FTP Request 
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(downhost + "/" + remoteFile);
            // Log in to the FTP Server with the User Name and Password Provided
            ftpRequest.Credentials = new NetworkCredential(downuser, downpass);
            // When in doubt, use these options
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;
            /* Set HTTP Proxy to Null to avoid The Requested FTP Command Is Not Supported When Using HTTP Proxy error */
            ftpRequest.Proxy = null;
            // Specify the Type of FTP Request
            ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
            // Establish Return Communication with the FTP Server
            ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
            // Get the FTP Server's Response Stream
            ftpStream = ftpResponse.GetResponseStream();
            // Open a File Stream to Write the Downloaded File
            FileStream localFileStream = new FileStream(localFile, FileMode.Create);
            // Buffer for the Downloaded Data 
            byte[] byteBuffer = new byte[bufferSize];
            int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
            // Download the File by Writing the Buffered Data Until the Transfer is Complete

            try
            {
                while (bytesRead > 0)
                {
                    localFileStream.Write(byteBuffer, 0, bytesRead);
                    bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
                }

            }

            catch (Exception ex) { Console.WriteLine(ex.ToString()); }
            // Resource Cleanup
            localFileStream.Close();
            ftpStream.Close();
            ftpResponse.Close();
            ftpRequest = null;

        }
        catch (Exception ex) { Console.WriteLine(ex.ToString()); }
        return;
    }

我使用在http://www.codeproject.com/Tips/443588/Simple-Csharp-FTP-Class找到的代码作为构建我的程序的基础,我已经完成了谷歌搜索其他人如何编写他们的ftp下载脚本,但无法找出任何理由为什么数据没有被写入。

感谢任何帮助。

4 个答案:

答案 0 :(得分:0)

刷新文件。在关闭之前请致电localFileStream.Flush();

答案 1 :(得分:0)

我意识到这有点晚了,但我在尝试使用该示例时遇到了同样的问题,我设法从这里获得了一个FTP下载示例:

http://www.techrepublic.com/blog/howdoi/how-do-i-use-c-to-upload-and-download-files-from-an-ftp-server/165

答案 2 :(得分:0)

我遇到了同样方法的问题。我确实找到了让系统运行的方法但我必须将代码中的ftpstream复制到另一个这样的流:(在vb.net中,我知道,对不起)

ftpStream.copyTo(MySecondStream)
ftpStream.close()
'do whatever you want with the copy now
Return MySecondStream

可能它与初始流的处理方式和保持打开的时间有关。我在这里发布了我的问题:Why do I have to copy an FTP stream to another variable to return it to the calling method?

答案 3 :(得分:0)

我在上传时遇到了同样的问题。写入0个字节。代码正在一个FTP服务器上运行,但在另一个FTP服务器上运行只需要做以下事情:

                client.FtpStream.Flush();
                client.FtpStream.Close();