Delphi FTP Upload Download

Source: Internet
Author: User

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.

Delphi FTP upload Download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.