Chilkat HOME ASP Visual Basic VB.NET C# Visual C++ C MFC Delphi FoxPro Java Perl PHP Python Ruby SQL Server VBScript
FTP Download a File with Progress Monitoring
C++ sample program to download a file from an FTP server with progress monitoring callbacks.
// To get FTP progress monitoring callbacks, declare a C++ class
// that inherits from the CkFtpProgress base class and provide
// implementations for FtpPercentDone and AbortCheck.
class MyFtpProgress : public CkFtpProgress
{
// pctDone is a percent value between 1 and 100.
// FtpPercentDone is called each time pctDone increases.
// The abort argument can be set to cause the FTP to abort while in progress.
void FtpPercentDone(long pctDone, bool *abort)
{
printf("%d Percent Done!\n",pctDone);
// In this example, the commented-out code will abort the transfer after it is 40% complete.
//if (pctDone >= 40)
//{
//*abort = true;
//}
}
// Called periodically to check to see if the transfer should be aborted.
void AbortCheck(bool *abort)
{
printf("Abort Check!\n");
// Set the *abort argument to true to force a transfer to abort.
// For example:
//*abort = true;
}
};
void FtpDownload(void)
{
CkFtp2 ftp;
MyFtpProgress myProgress;
ftp.UnlockComponent("anything for 30-day trial");
ftp.put_Hostname("ftp.chilkatsoft.com");
ftp.put_Username("***");
ftp.put_Password("***");
// non-passive (active) connections are most likely to work if firewalls are present.
// However, if the data connection cannot be established, try switching to Passive mode instead.
ftp.put_Passive(false);
// The HeartbeatMs property controls the frequency of AbortCheck callbacks.
// The default value is 0 (no callbacks). Set it to a reasonable number of milliseconds:
ftp.put_HeartbeatMs(250);
// Some FTP server may require that the AutoGetSizeForProgress property be set to true.
// Only do this if you see that percent-done progress events are not firing.
ftp.put_AutoGetSizeForProgress(true);
bool success = ftp.Connect();
if (!success)
{
ftp.SaveLastError("ftpLog.txt");
return;
}
// Call GetFile2 to download with progress monitoring.
// Call GetFile to download without progress monitoring.
success = ftp.GetFile2("someFile.dat","someFile.dat",myProgress);
if (!success)
{
ftp.SaveLastError("ftpLog.txt");
}
ftp.Disconnect();
}
|
© 2000-2010 Chilkat Software, Inc. All Rights Reserved.