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 <C_CkFtp2W.h>

BOOL ftp_PercentDone(int percentDone)
    {
    wprintf(L"Percent Done: %d\n",percentDone);
    return FALSE;
    }

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkFtp2W ftp;
    BOOL (*fnftpPercentDone)(int percentDone) = ftp_PercentDone;
    void (*fnftpProgressInfo)(const wchar_t *name, const wchar_t *value) = ftp_ProgressInfo;
    const wchar_t *localFilename;
    const wchar_t *remoteFilename;

    success = FALSE;

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

    ftp = CkFtp2W_Create();
    CkFtp2W_setPercentDone(ftp, fnftpPercentDone);
    CkFtp2W_setProgressInfo(ftp, fnftpProgressInfo);

    CkFtp2W_putHostname(ftp,L"ftp.someFtpServer.com");
    CkFtp2W_putUsername(ftp,L"myLogin");
    CkFtp2W_putPassword(ftp,L"myPassword");

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

    success = CkFtp2W_LoginAfterConnectOnly(ftp);
    if (success != TRUE) {
        wprintf(L"%s\n",CkFtp2W_lastErrorText(ftp));
        CkFtp2W_Dispose(ftp);
        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 = CkFtp2W_ChangeRemoteDir(ftp,L"junk");
    if (success != TRUE) {
        wprintf(L"%s\n",CkFtp2W_lastErrorText(ftp));
        CkFtp2W_Dispose(ftp);
        return;
    }

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

    // Ensure that we get PercentDone callbacks.
    CkFtp2W_putAutoGetSizeForProgress(ftp,TRUE);

    // Download the file.
    success = CkFtp2W_GetFile(ftp,remoteFilename,localFilename);
    if (success != TRUE) {
        wprintf(L"%s\n",CkFtp2W_lastErrorText(ftp));
        CkFtp2W_Dispose(ftp);
        return;
    }

    success = CkFtp2W_Disconnect(ftp);

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


    CkFtp2W_Dispose(ftp);

    }