Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

FTP Download File to a Stream

Demonstrates how to FTP download a file to a Chilkat stream.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Ftp2,
  Chilkat.Stream;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  ftp: TFtp2;
  streamObj: TStream;

begin
  success := False;

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

  ftp := TFtp2.Create;

  ftp.Hostname := 'my-ftp-server.com';
  ftp.Port := 21;
  ftp.Username := 'mFtpLogin';
  ftp.Password := 'myFtpPassword';
  ftp.AuthTls := True;
  ftp.Passive := True;

  //  Connect and login to the FTP server using TLS.
  success := ftp.Connect();
  if (success <> True) then
    begin
      WriteLn(ftp.LastErrorText);
      Exit;
    end;

  //  Move to the sub-directory (from the FTP user's home directory) where the file is located.
  success := ftp.ChangeRemoteDir('temp');
  if (success = False) then
    begin
      WriteLn(ftp.LastErrorText);
      Exit;
    end;

  //  Stream to this local file:
  streamObj := TStream.Create;
  streamObj.SinkFile := 'c:/temp/qa_output/penguins2.jpg';

  success := ftp.GetFileToStream('penguins2.jpg',streamObj);
  if (success = False) then
    begin
      WriteLn(ftp.LastErrorText);
      Exit;
    end;

  ftp.Disconnect();

  WriteLn('Success.');


  ftp.Free;
  streamObj.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.