Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
FTP Upload / Download to StringBuilder
Demonstrate how to 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.Ftp2,
Chilkat.StringBuilder;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
ftp: TFtp2;
sbA: TStringBuilder;
bIncludeBOM: Boolean;
remoteFilename: string;
sbB: TStringBuilder;
bCaseSensitive: Boolean;
begin
success := False;
// This example assumes Chilkat Ftp2 to have been previously unlocked.
// See Unlock Ftp2 for sample code.
ftp := TFtp2.Create;
ftp.Hostname := 'www.my-ftp-server.com';
ftp.Username := 'mFtpLogin';
ftp.Password := 'myFtpPassword';
// Connect to the FTP server.
success := ftp.ConnectOnly();
if (success <> True) then
begin
WriteLn(ftp.LastErrorText);
Exit;
end;
// Authenticate with the FTP server.
success := ftp.LoginAfterConnectOnly();
if (success <> True) then
begin
WriteLn(ftp.LastErrorText);
Exit;
end;
sbA := TStringBuilder.Create;
sbA.LoadFile('qa_data/hamlet.xml','utf-8');
// Upload the contents of sbA to the FTP server.
bIncludeBOM := False;
remoteFilename := 'hamletFromSb.xml';
success := ftp.PutFileSb(sbA,'utf-8',bIncludeBOM,remoteFilename);
if (success <> True) then
begin
WriteLn(ftp.LastErrorText);
Exit;
end;
// Download...
sbB := TStringBuilder.Create;
success := ftp.GetFileSb(remoteFilename,'utf-8',sbB);
if (success <> True) then
begin
WriteLn(ftp.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;
ftp.Disconnect();
ftp.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.