How to Download a File via FTP in Delphi XE

Posted on November 6, 2020 at 3:35 pm

A quick example to download a fila via FTP using Indy (IdFTP):

uses
 ... IdFTP, IdFTPCommon ...
 
procedure TForm1.Button1Click(Sender: TObject);
begin
  IdFTP1.Host := 'test.rebex.net';
  IdFTP1.Port := 21;
  IdFTP1.ConnectTimeout := 5000;
  IdFTP1.Username := 'demo';
  IdFTP1.Password := 'password';
  IdFTP1.TransferType := IdFTPCommon.TIdFTPTransferType.ftBinary;
  IdFTP1.Passive := True;
 
  try
    IdFTP1.Connect;
    try
      IdFTP1.Get('/pub/example/pop3-browser.png', ExtractFilePath(ParamStr(0)) + 'pop3-browser.png', True, False);
    finally
      IdFTP1.Disconnect;
    end;
    ShowMessage('Downloaded!');
  except
    ShowMessage('Error while downloading!');
  end;
end;

If the FTP server allows only anonymous connections use:

IdFTP1.Username := 'anonymous';
//IdFTP1.Password := 'password';

You can test an anonymous FTP connection via:

ftp://speedtest.tele2.net/

If you need to parse the FTP URL check this:

Parse or Split FTP URL in Delphi XE using TIdURI

Receive updates via email

Other Posts

Updated Posts