Since Delphi itself has Indy controls, it is fast and easy to use these controls for network programming. Nonetheless, I have greatly encouraged myself to see how the FTP bottom layer is implemented, but for now, the Indy control is a very good choice.
For the strong Indy, I still decided to post this code, mainly to facilitate their future review, query.
Unit1;
Interface
Uses
Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms,
Dialogs, Stdctrls, Idbasecomponent, Idcomponent, Idtcpconnection,
Idtcpclient, idftp;
Type
TForm1 = Class (Tform)
Idftp1:tidftp;
Button1:tbutton;
Memo1:tmemo;
Edit1:tedit;
Button2:tbutton;
Label1:tlabel;
Button3:tbutton;
Opendialog1:topendialog;
Procedure Button1Click (Sender:tobject);
Procedure Button2click (Sender:tobject);
Procedure Button3click (Sender:tobject);
Procedure Idftp1status (asender:tobject; const astatus:tidstatus;
Const astatustext:string);
Private
{Private declarations}
Public
{Public declarations}
End
Var
Form1:tform1;
Implementation
Uses idftplist, Idftpcommon;
{$R *.DFM}
{
This program is an FTP demo program, I use the Windows Information service in this machine set up an FTP service,
Native ip:192.168.10.99
User name: CC
Password: 1
}
Procedure Tform1.button1click (Sender:tobject);
Var
Tr:tstrings;
Begin//Connect
TR: = Tstringlist.create;
Idftp1.host: = ' 192.168.10.99 '; The FTP server IP address or domain name can also be
Idftp1.username: = ' Username '; FTP Server user Name
Idftp1.password: = ' Password '; FTP Server password
Idftp1.connect (); Connect to FTP
Edit1.text: = idftp1.retrievecurrentdir;//Get initial directory
Idftp1.changedir (' client '); Go to the client sub-directory
Idftp1.changedir ('.. '); Back to the top level directory
Idftp1.list (TR); Get a list of all files in the client directory
Memo1.Lines.Assign (TR);
Tr. Free;
End
Procedure Tform1.button2click (Sender:tobject);
Var
Tt:tidftplistitems;
T:tidftplistitem;
I:integer;
tfname:string;
Begin//download
Label1.Caption: = Idftp1.directorylisting.items[0]. FileName;
Idftp1.transfertype: = ftbinary; Specified as a binary file or text file Ftascii
For i:=0 to Idftp1.directorylisting.count-1 do
Begin
TT: = idftp1.directorylisting; Get a list of files and directories in the current directory
T: = TT. Items[i]; Get a file related information
Label1.Caption: =t.text; Take out a file information content
Tfname: = T.filename;
ShowMessage (t.ownername+ "+t.groupname+" +t.filename+ "+t.linkeditemname);
If Idftp1.directorylisting.items[i]. ItemType = Ditfile then//If it is a file
Begin
Idftp1.get (tfname, ' d:\FTPtest\ ' +tfname,true,true); Download to local, overwrite, and support breakpoint continuation
End
End
End
Procedure Tform1.button3click (Sender:tobject);
Var
fi:string;
Begin//Upload
If Opendialog1.execute Then
Begin
Fi: = Opendialog1.filename;
Idftp1.put (' f:\ test document Sample. rar ', ' Test document Sample. rar ');//Upload,
End
End
Procedure Tform1.idftp1status (asender:tobject; const astatus:tidstatus;
Const astatustext:string);
Begin
{Case Astatus of
Hsresolving:showmessage (' hsresolving ');
Hsconnecting:showmessage (' hsconnecting ');
Hsconnected:showmessage (' hsconnected ');
Hsdisconnecting:showmessage (' hsdisconnecting ');
Hsdisconnected:showmessage (' hsdisconnected ');
Hsstatustext:showmessage (' Hsstatustext ');
Ftptransfer:showmessage (' File transfer complete. ‘);
Ftpready:showmessage (' Prepare to transfer files ... ');
Ftpaborted:showmessage (' transmit failed ');
End }
ShowMessage (Astatustext);
End
End.
Examples of FTP upload and download in Delphi using Indy