0

I'm trying to download ftp some file from a remote server using OpenNETCF.ftp class File size 96Kb I get an error while opening it says Corrupt File

private static int BUFFER_SIZE = 512;
private static int DEFAULT_PORT = 21;

private bool                m_connected     = false;
private FTPMode             m_mode          = FTPMode.Passive;
private int                 m_port = DEFAULT_PORT;
private string              m_host          = "";
private FTPTransferType m_type = FTPTransferType.Binary;
private string              m_uid           = "";
private string              m_pwd           = "";
private Socket              m_cmdsocket     = null;
private bool                m_exceptions    = true;
private byte[]              m_buffer        = new byte[BUFFER_SIZE];
private FTPServerType       m_server = FTPServerType.Unknown;

public void GetFile(string remoteFileName, string localFileName, bool overwrite)
{
    int bytesrecvd = 0;
    using (var output = File.Create(localFileName))
    using (var socket = OpenDataSocket())
    {
        response = SendCommand("RETR " + remoteFileName);
        if (!((response.ID == StatusCode.FileStatusOK) || (response.ID == StatusCode.ConnectionAlreadyOpen)))
        {
            if (!m_exceptions)
           {
               return;
           }
           else
           {
               throw new IOException(response.Text);
           }
       }

   while (true)
   {
       bytesrecvd = socket.Receive(m_buffer,m_buffer.Length, 0);
       output.Write(m_buffer, 0, bytesrecvd);
       response.Text = bytesrecvd.ToString();
       if (bytesrecvd <= 0)
       {
           break;
       }
   }
}
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
xfiles
  • 1
  • 2

1 Answers1

0

Most likely you are not setting binary mode for transfer, and by default it is set to the ASCII. You need to issue 'TYPE I' command.

Nickolay Olshevsky
  • 13,706
  • 1
  • 34
  • 48