Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
SFTP Upload and Download to a StringBuilder Object
See more SFTP Examples
Demonstrates how to SFTP upload from a Chilkat StringBuilder object, and download into a StringBuilder object.Chilkat Pascal (Lazarus/Delphi) Downloads
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.StringBuilder;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
sftp: TSFtp;
port: Integer;
sbA: TStringBuilder;
bIncludeBOM: Boolean;
remotePath: string;
sbB: TStringBuilder;
bCaseSensitive: Boolean;
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;
sbA := TStringBuilder.Create;
sbA.LoadFile('qa_data/hamlet.xml','utf-8');
// Upload the contents of sbA to the SSH/SFTP server.
bIncludeBOM := False;
remotePath := 'sftpTesting/hamlet.xml';
success := sftp.UploadSb(sbA,remotePath,'utf-8',bIncludeBOM);
if (success <> True) then
begin
WriteLn(sftp.LastErrorText);
Exit;
end;
// Download the file..
sbB := TStringBuilder.Create;
success := sftp.DownloadSb(remotePath,'utf-8',sbB);
if (success <> True) then
begin
WriteLn(sftp.LastErrorText);
Exit;
end;
// Verify that sbA and sbB have the exact same contents.
WriteLn('size of sbA: ' + sbA.Length);
bCaseSensitive := True;
if (sbA.ContentsEqualSb(sbB,bCaseSensitive) = True) then
begin
WriteLn('Contents are equal. Success.');
end
else
begin
WriteLn('Contents are NOT equal. Failed.');
end;
sftp.Disconnect();
sftp.Free;
sbA.Free;
sbB.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.