delphi achieve FTP upload and download

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP;

type
  TForm1 = class(TForm)
    IdFTP1: TIdFTP;
    dlgOpen1: TOpenDialog;
    btn1: TButton;
    mmo1: TMemo;
    btn2: TButton;
    btn3: TButton;
    edt1: TEdit;
    procedure btn1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure btn3Click(Sender: TObject);
    procedureIdFTP1Status (ASender: TObject; const AStatus: TIdStatus;
       const AStatusText: String);
   Private 
    { Private Declarations } 
  public 
    { Public Declarations } 
  End ; 

var 
  the Form1: TForm1; 

Implementation 

{ $ R & lt *. Dfm } 
uses   IdFTPList, IdFTPCommon; 

// this uploads and downloads are pure to achieve the download and upload with idftp client functionality is not written something ftp server-side 

{ 
 this program is a ftp demo program, I use windows information service set up a ftp service on this machine, 
 this machine IP : 192.168.10.99 
 username: cc my 
 password: QWERT. 1 
} 
Procedure TForm1.btn1Click (Sender: TObject);
var 
  TR: Tstrings; 
the begin   // connected to 
  TR:. = TStringList the Create ;
   // IdFTP1.Host: = '192.168.10.99'; // address of the FTP server 
  IdFTP1.Host: = ' 127.0.0.1 ' ; // the FTP server address 
  IdFTP1.Port: = 2121 ; // Note the port number, this is not the default of 21, but the value of your own set 
  IdFTP1.Username: = ' My ' ; // the FTP server user name 
  IdFTP1.Password: = ' QWERT ' ; // the FTP server password 

  IdFTP1.Connect ();   // connect to the FTP 

  edt1.Text:IdFTP1.RetrieveCurrentDir =; // get initial directory 
  // IdFTP1.ChangeDir ( 'client'); // into the client subdirectory 
  // IdFTP1.ChangeDir ( '..'); // Return the parent directory 
  IdFTP1. list (TR); // get the list of all files under the directory client 
  mmo1.Lines.Assign (TR); 
  tr.Free; 
End ; 
 


Procedure TForm1.btn2Click (Sender: TObject);
 var 
  TT: TIdFTPListItems; 
  T: TIdFTPListItem; 
  I: Integer; 
  tfname: String; 
the begin   // Download 
  // Label1.Caption: = IdFTP1.DirectoryListing.Items [0] .FileName; 
  IdFTP1.TransferType: = ftBinary; // specified in binary or text file ftASCII 
  forI: = 0  to IdFTP1.DirectoryListing.Count- . 1  do 
  the begin 
    TT: = IdFTP1.DirectoryListing; // get current directory file and directory listing 
    T: = tt.Items [I]; // get information about a file 
   // Label1.Caption: = t.Text; // remove a file content 
    tfname: = t.FileName;
    // ShowMessage (t.OwnerName + '' + t.GroupName + '' + t.FileName + '' + t.LinkedItemName); 
    IF IdFTP1.DirectoryListing.Items [I] = ditFile .ItemType the then  // if the file is 
    the begin 
      IdFTP1.Get (tfname, ExtractFilePath (Application.ExeName) + tfname, True, True); // downloaded locally, and to cover, and support for HTTP
    End ;
   End ;
 End ; 

Procedure TForm1.btn3Click (Sender: TObject);
 var 
  Fi: String ;
 the begin   // upload 
  IF dlgOpen1.Execute the then 
  the begin 
     Fi: = dlgOpen1.FileName;
     // IdFTP1.Put ( 'F: / test document sample .rar ',' test document samples .rar '); // uploaded, 
      IdFTP1.Put (fi, ExtractFileName (fi));
   End ;
 End ; 



Procedure TForm1.IdFTP1Status (ASender: TObject; const AStatus: TIdStatus;
   const AStatusText : String);
 the begin 
  {case  AStatus of
    hsResolving  : showmessage('hsResolving');
    hsConnecting: showmessage('hsConnecting');
    hsConnected: showmessage('hsConnected');
    hsDisconnecting: showmessage('hsDisconnecting');
    hsDisconnected: showmessage('hsDisconnected');
    hsStatusText: showmessage('hsStatusText');  
    ftpTransfer: showmessage('文件传送完毕。');
    ftpReady: showmessage('准备传送文件....');
    ftpAborted: showmessage('传送失败');
  end;         }

  //showmessage(AStatusText);
end;
 


end.

Guess you like

Origin www.cnblogs.com/tobetterlife/p/12169488.html