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

SFTP Upload and Download to a BinData Object

See more SFTP Examples

Demonstrates how to SFTP upload from a BinData object, and download into a BinData object.

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.SFtp,
  Chilkat.BinData;

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

procedure RunDemo;
var
  success: Boolean;
  sftp: TSFtp;
  port: Integer;
  bdA: TBinData;
  remotePath: string;
  bdB: TBinData;

begin
  success := False;

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

  sftp := TSFtp.Create;

  //  Set some timeouts, in milliseconds:
  sftp.ConnectTimeoutMs := 5000;
  sftp.IdleTimeoutMs := 10000;

  //  Connect to the SSH server and then authenticate.
  port := 22;
  success := sftp.Connect('MY-SSH-SERVER-DOMAIN-OR-IP',port);
  if (success = True) then
    begin
      success := sftp.AuthenticatePw('MY-SSH-LOGIN','MY-SSH-PASSWORD');
      if (success = True) then
        begin
          success := sftp.InitializeSftp();
        end;
    end;
  if (success <> True) then
    begin
      WriteLn(sftp.LastErrorText);
      Exit;
    end;

  bdA := TBinData.Create;
  bdA.LoadFile('qa_data/jpg/penguins.jpg');

  //  Upload the contents of bdA to the SSH/SFTP server.
  remotePath := 'sftpTesting/penguins.jpg';
  success := sftp.UploadBd(bdA,remotePath);
  if (success <> True) then
    begin
      WriteLn(sftp.LastErrorText);
      Exit;
    end;

  //  Download the file..
  bdB := TBinData.Create;
  success := sftp.DownloadBd(remotePath,bdB);
  if (success <> True) then
    begin
      WriteLn(sftp.LastErrorText);
      Exit;
    end;

  //  Verify that bdA and bdB have the exact same contents.
  WriteLn('size of bdA: ' + bdA.NumBytes);
  if (bdA.ContentsEqual(bdB) = True) then
    begin
      WriteLn('Contents are equal. Success.');
    end
  else
    begin
      WriteLn('Contents are NOT equal.  Failed.');
    end;

  sftp.Disconnect();


  sftp.Free;
  bdA.Free;
  bdB.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.