Perl
Chilkat2 Perl
FTP Download with Progress Event Callbacks
See more FTP Examples
FTP download with progress event callbacks.Chilkat Perl Downloads
use strict;
use warnings;
use Chilkat2;
# This example requires the Chilkat API to have been previously unlocked.
# See Global Unlock Sample for sample code.
my $ftp = Chilkat2::Ftp2->new;
$ftp->on(
PercentDone => sub {
my ($percentDone) = @_;
print 'Percent Done: ', $percentDone, "\n";
return 0; # return 1 to abort the method in progress
},
ProgressInfo => sub {
my ($name, $value) = @_;
print $name, ': ', $value, "\n";
}
);
$ftp->Hostname('ftp.someFtpServer.com');
$ftp->Username('myLogin');
$ftp->Password('myPassword');
# Connect and login to the FTP server.
my $success = $ftp->ConnectOnly();
if (!$success) {
print $ftp->LastErrorText, "\n";
exit;
}
$success = $ftp->LoginAfterConnectOnly();
if (!$success) {
print $ftp->LastErrorText, "\n";
exit;
}
# 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('junk');
if (!$success) {
print $ftp->LastErrorText, "\n";
exit;
}
my $localFilename = 'c:/temp/hamlet.xml';
my $remoteFilename = 'hamlet.xml';
# Ensure that we get PercentDone callbacks.
$ftp->AutoGetSizeForProgress(1);
# Download the file.
$success = $ftp->GetFile($remoteFilename, $localFilename);
if (!$success) {
print $ftp->LastErrorText, "\n";
exit;
}
$success = $ftp->Disconnect();
print "File Downloaded!\n";