How to download?

Feb 26, 2013 at 1:33 PM
Hi, i have tried for a few hours to download a file from an ftp server,

but so far i get 0 byte files (they are 300k on the server)

The example for downloading the files (Openread) seems to lack the actually writing-to-disk-part

This is my function that supposively should write the files to disk.
  private void Download(string fileName, string localPath) {
    //MessageBox.Show( fileName);
        
        using (FtpClient conn = new FtpClient())
        {
                conn.Host = ftpHost;
                conn.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
                
           


                using (Stream istream = conn.OpenRead(fileName))
                    {
                    try {
                        // istream.Position is incremented accordingly to the reads you perform
                        // istream.Length == file size if the server supports getting the file size
                        // also note that file size for the same file can vary between ASCII and Binary
                        // modes and some servers won't even give a file size for ASCII files! It is
                        // recommended that you stick with Binary and worry about character encodings
                        // on your end of the connection.
                    FileStream file = new FileStream(localPath, FileMode.Create, System.IO.FileAccess.Write);
                    byte[] bytes = new byte[istream.Length];
                    istream.Read(bytes, 0, (int)istream.Length);
                    file.Write(bytes, 0, bytes.Length);
                    file.Close();

                    }
                    finally {

                    
                        
                        
                        istream.Close();
                    }
                }
                }
    }
Coordinator
Feb 26, 2013 at 2:20 PM
You need to use a loop to read the stream until 0 is returned. You also only need to write the number of bytes returned by istream.Read(), not the length of the buffer as your buffer may not be filled and that will surely corrupt the file.
Feb 27, 2013 at 5:43 AM
jptrosclair wrote:
You need to use a loop to read the stream until 0 is returned. You also only need to write the number of bytes returned by istream.Read(), not the length of the buffer as your buffer may not be filled and that will surely corrupt the file.
Thank you, i re-wrote the "save" part to this:
              using (Stream istream = conn.OpenRead(fileName))
                    {
                    FileStream fileoutput = new FileStream(localPath, FileMode.Create, System.IO.FileAccess.Write);

                    try
                        {
                        // istream.Position is incremented accordingly to the reads you perform
                        // istream.Length == file size if the server supports getting the file size
                        // also note that file size for the same file can vary between ASCII and Binary
                        // modes and some servers won't even give a file size for ASCII files! It is
                        // recommended that you stick with Binary and worry about character encodings
                        // on your end of the connection.



                        byte[] buffer = new byte[8 * 1024];
                        int len;
                        while ((len = istream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                            fileoutput.Write(buffer, 0, len);
                            }
                        }
                    finally
                        {


                        fileoutput.Close();
                        istream.Close();
                        }
                    }
 
And it now downloads and saves the file(s) correctly.