Sample code for 30+ languages & platforms
Unicode C++

FTP Download with Progress Event Callbacks

See more FTP Examples

FTP download with progress event callbacks.

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkFtp2W.h>

#include <CkFtp2ProgressW.h>

class ftpProgress : public CkFtp2ProgressW {

  public:
  ftpProgress() { }
  virtual ~ftpProgress() { }

  void PercentDone(int percentDone, bool *abort) {
    wprintf(L"Percent Done: %d\n",percentDone);
  }

  void ProgressInfo(const wchar_t *name, const wchar_t *value) {
    wprintf(L"%s: %s\n",name,value);
  }

};

void ChilkatSample(void)
    {
    bool success = false;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkFtp2W ftp;
    ftpProgress ftp_progress;
    ftp.put_EventCallbackObject(&ftp_progress);

    ftp.put_Hostname(L"ftp.someFtpServer.com");
    ftp.put_Username(L"myLogin");
    ftp.put_Password(L"myPassword");

    // Connect and login to the FTP server.
    success = ftp.ConnectOnly();
    if (success != true) {
        wprintf(L"%s\n",ftp.lastErrorText());
        return;
    }

    success = ftp.LoginAfterConnectOnly();
    if (success != true) {
        wprintf(L"%s\n",ftp.lastErrorText());
        return;
    }

    // Change to the remote directory where the file is located.
    // This step is only necessary if the file is not in the home directory
    // of the FTP account.
    success = ftp.ChangeRemoteDir(L"junk");
    if (success != true) {
        wprintf(L"%s\n",ftp.lastErrorText());
        return;
    }

    const wchar_t *localFilename = L"c:/temp/hamlet.xml";
    const wchar_t *remoteFilename = L"hamlet.xml";

    // Ensure that we get PercentDone callbacks.
    ftp.put_AutoGetSizeForProgress(true);

    // Download the file.
    success = ftp.GetFile(remoteFilename,localFilename);
    if (success != true) {
        wprintf(L"%s\n",ftp.lastErrorText());
        return;
    }

    success = ftp.Disconnect();

    wprintf(L"File Downloaded!\n");
    }