Chilkat HOME Android™ Classic ASP C C++ C# Mono C# .NET Core C# C# UWP/WinRT DataFlex Delphi ActiveX Delphi DLL Visual FoxPro Java Lianja MFC Objective-C Perl PHP ActiveX PHP Extension PowerBuilder PowerShell PureBasic CkPython Chilkat2-Python Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ Visual Basic 6.0 VB.NET VB.NET UWP/WinRT VBScript Xojo Plugin Node.js Excel Go
(MFC) Asynchronous FTP DownloadThe Chilkat FTP component supports asynchronous uploads and downloads. This examples demonstrates doing a download in a background thread. The download is started by calling AsyncGetFileStart. While the download is in progress, your program may do other tasks. You may periodically check the AsyncFinished property to determine when the transfer is finished. The example below provides the remainder of the details.
#include <CkFtp2.h> void ChilkatSample(void) { CkString strOut; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. CkFtp2 ftp; ftp.put_Hostname("ftp.myftpserver.com"); ftp.put_Username("test"); ftp.put_Password("test"); // Connect and login to the FTP server. bool success = ftp.Connect(); if (success != true) { strOut.append(ftp.lastErrorText()); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } const char *localFilename = "hamlet.xml"; const char *remoteFilename = "hamlet.xml"; // Begin the download in a background thread. // Only 1 background upload or download may be active at any time. // (per instance of an FTP object) success = ftp.AsyncGetFileStart(remoteFilename,localFilename); if (success != true) { strOut.append(ftp.lastErrorText()); strOut.append("\r\n"); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); return; } // The application is now free to do anything else // while the file is downloading. // For this example, we'll simply sleep and periodically // check to see if the transfer if finished. While checking // however, we'll report on the progress in both number // of bytes tranferred and performance in bytes/second. while (ftp.get_AsyncFinished() != true) { strOut.appendInt(ftp.get_AsyncBytesReceived()); strOut.append(" bytes received"); strOut.append("\r\n"); strOut.appendInt(ftp.get_DownloadTransferRate()); strOut.append(" bytes per second"); strOut.append("\r\n"); // Sleep 1 second. ftp.SleepMs(1000); } strOut.appendInt(ftp.get_AsyncBytesReceived()); strOut.append(" total bytes received"); strOut.append("\r\n"); strOut.appendInt(ftp.get_DownloadTransferRate()); strOut.append(" final bytes per second"); strOut.append("\r\n"); // Did the download succeed? if (ftp.get_AsyncSuccess() == true) { strOut.append("File Downloaded!"); strOut.append("\r\n"); } else { // The error information for asynchronous ops // is in AsyncLog as opposed to LastErrorText strOut.append(ftp.asyncLog()); strOut.append("\r\n"); } success = ftp.Disconnect(); SetDlgItemText(IDC_EDIT1,strOut.getUnicode()); } |
||||
© 2000-2022 Chilkat Software, Inc. All Rights Reserved.