Sample code for 30+ languages & platforms
Delphi DLL

Download an FTP File as Text with a Charset

See more FTP Examples

Demonstrates the Chilkat Ftp2.GetRemoteFileTextC method, which downloads a remote file, decodes the bytes using a named charset, and returns the text. The first argument is the remote filename and the second is the charset. A byte-order mark for the selected encoding is recognized and omitted.

Background: This is the encoding-aware way to read a remote text file: you name the charset (utf-8, iso-8859-1, and so on) so multibyte and accented characters decode correctly, and a leading BOM is stripped automatically. Prefer it over GetRemoteFileTextData whenever the file's encoding is known, which is almost always.

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
ftp: HCkFtp2;
fileText: PWideChar;

begin
success := False;

//  Demonstrates the Ftp2.GetRemoteFileTextC method, which downloads a remote file, decodes the
//  bytes using a named charset, and returns the text.  The 1st argument is the remote filename
//  and the 2nd is the charset.

ftp := CkFtp2_Create();

CkFtp2_putHostname(ftp,'ftp.example.com');
CkFtp2_putUsername(ftp,'myFtpLogin');

//  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.
CkFtp2_putPassword(ftp,'myPassword');

success := CkFtp2_Connect(ftp);
if (success = False) then
  begin
    Memo1.Lines.Add(CkFtp2__lastErrorText(ftp));
    Exit;
  end;

//  Download a UTF-8 text file.  A byte-order mark for the selected encoding is recognized and
//  omitted from the returned text.
fileText := CkFtp2__getRemoteFileTextC(ftp,'public_html/readme.txt','utf-8');
if (CkFtp2_getLastMethodSuccess(ftp) = False) then
  begin
    Memo1.Lines.Add(CkFtp2__lastErrorText(ftp));
    Exit;
  end;
Memo1.Lines.Add(fileText);

success := CkFtp2_Disconnect(ftp);
if (success = False) then
  begin
    Memo1.Lines.Add(CkFtp2__lastErrorText(ftp));
    Exit;
  end;

CkFtp2_Dispose(ftp);