简体   繁体   中英

FTP download not writing the data in the file

I have written a program that downloads a file from a remote ftp site and saves it to the local c drive then uploads that file to a separate server. The problem now is that when the file gets downloaded, there is no data inside the text file it creates on the local C and I can't figure out why that is. Here is the code I'm using

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

I used the code found at http://www.codeproject.com/Tips/443588/Simple-Csharp-FTP-Class as the basis to build my program off of and I've done Google searches on how other people have written their ftp download scripts but can't figure out any reason why the data isn't being written.

Any help is appreciated.

Flush the file. Call localFileStream.Flush(); before you close it.

I realise that this is a bit late, but i was having the same issue trying to use that example, i managed to get a FTP download working with an example from here:

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

I'm having issues with the same method. I did find a way to get the system to work but I had to copy the ftpstream in your code to another stream like this: (in vb.net, I know, I'm sorry)

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

Maybe it has something to do with how the initial stream is handled and how long it stays open. I posted my question here: Why do I have to copy an FTP stream to another variable to return it to the calling method?

I had the same problem on an upload. 0 bytes written. Code was working on one FTP server, but not on another. Just had to do the following:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM