To perform FTP operations in Delphi, you can use the TIdFTP component from the Indy library. The Indy library is a set of Delphi components that support a variety of Internet protocols, including FTP.
To install the Indy library, follow these steps:
- Download the Indy library from the following URL: https://www.indyproject.org/download/Sockets.zip
- Extract the downloaded archive to a folder on your computer.
- Open Delphi and go to the “Component” menu.
- Choose “Install Packages” and click on the “Add” button.
- Navigate to the folder where you extracted the Indy library and select the “indy*.bpl” file.
- Click on the “Open” button to install the Indy library.
- With the Indy library installed, you can now use the TIdFTP component in your Delphi code.
Here is an example of how to use the component to upload a file to an FTP server:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
uses IdFTP; procedure UploadFile(const AFileName, ADestination: string); var FTP: TIdFTP; begin FTP := TIdFTP.Create(nil); try FTP.Host := ‘ftp.example.com’; FTP.Username := ‘username’; FTP.Password := ‘password’; FTP.Connect; try FTP.Put(AFileName, ADestination); finally FTP.Disconnect; end; finally FTP.Free; end; end; |
To download a file from an FTP server, you can use the Get method of the TIdFTP component, like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
uses IdFTP; procedure DownloadFile(const ASource, ADestination: string); var FTP: TIdFTP; begin FTP := TIdFTP.Create(nil); try FTP.Host := ‘ftp.example.com’; FTP.Username := ‘username’; FTP.Password := ‘password’; FTP.Connect; try FTP.Get(ASource, ADestination); finally FTP.Disconnect; end; finally FTP.Free; end; end; |
To list the files on an FTP server, you can use the List method of the TIdFTP component, like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
uses IdFTP; procedure ListFiles(const ADirectory: string); var FTP: TIdFTP; List: TStrings; I: Integer; begin FTP := TIdFTP.Create(nil); try FTP.Host := ‘ftp.example.com’; FTP.Username := ‘username’; FTP.Password := ‘password’; FTP.Connect; try List := TStringList.Create; try FTP.List(List, ADirectory); for I := 0 to List.Count – 1 do Writeln(List[I]); finally List.Free; end; finally FTP.Disconnect; end; |
Leave a Reply