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

Read Bytes from a Remote SFTP File (BinData)

See more SFTP Examples

Demonstrates the Chilkat SFtp.ReadFileBd method, which reads up to a number of bytes from the current position of an open handle and appends them to a BinData. The arguments are the handle, the maximum number of bytes, and the BinData. This example reads in chunks until end-of-file.

Background: For binary files, reading into a BinData preserves the bytes exactly, unlike a text read that decodes through a charset. Because it appends, the chunked read loop accumulates the whole file in the BinData; each pass reads up to the chunk size and the loop repeats until Eof. Fewer bytes than requested can come back near the end of the file, which is normal.

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;
  password: string;
  handle: string;
  bd: TBinData;
  chunkSize: Integer;
  reading: Boolean;

begin
  success := False;

  //  Demonstrates the SFtp.ReadFileBd method, which reads up to a number of bytes from the current
  //  position of an open handle and appends them to a BinData.  The 1st argument is the handle, the
  //  2nd is the maximum number of bytes, and the 3rd is the BinData.

  sftp := TSFtp.Create;

  //  Connect, authenticate, and initialize the SFTP subsystem.
  port := 22;
  success := sftp.Connect('sftp.example.com',port);
  if (success = False) then
    begin
      WriteLn(sftp.LastErrorText);
      Exit;
    end;

  //  Normally you would not hard-code the password in source.  You should instead obtain it
  //  from an interactive prompt, environment variable, or a secrets vault.
  password := 'mySshPassword';

  success := sftp.AuthenticatePw('mySshLogin',password);
  if (success = False) then
    begin
      WriteLn(sftp.LastErrorText);
      Exit;
    end;

  success := sftp.InitializeSftp();
  if (success = False) then
    begin
      WriteLn(sftp.LastErrorText);
      Exit;
    end;

  handle := sftp.OpenFile('subdir/image.png','readOnly','openExisting');
  if (sftp.LastMethodSuccess = False) then
    begin
      WriteLn(sftp.LastErrorText);
      Exit;
    end;

  //  Read the file in chunks until end-of-file, accumulating the bytes in a BinData.
  bd := TBinData.Create;
  chunkSize := 8192;
  reading := True;
  while reading do
    begin
      success := sftp.ReadFileBd(handle,chunkSize,bd);
      if (sftp.LastReadFailed(handle)) then
        begin
          WriteLn(sftp.LastErrorText);
          Exit;
        end;

      reading := not sftp.Eof(handle);
    end;

  success := sftp.CloseHandle(handle);
  if (success = False) then
    begin
      WriteLn(sftp.LastErrorText);
      Exit;
    end;
  WriteLn('Read ' + bd.NumBytes + ' bytes.');

  sftp.Disconnect();


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