delphi实现FTP上传与下载

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);
    procedure IdFTP1Status(ASender: TObject; const AStatus: TIdStatus;
      const AStatusText: String);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
uses  IdFTPList, IdFTPCommon;

//这个上传和下载 是 纯用 idftp 客户端 来达到 下载和上传的功能, 没有写到ftp服务端的东西

{
 本程序是一个ftp  demo程序,我用windows信息服务在本机建立了一个ftp服务,
 本机IP:192.168.10.99
 用户名:cc  my
 密码:1  qwert
}
procedure TForm1.btn1Click(Sender: TObject);
var
  tr : Tstrings;
begin  //连接
  tr := TStringlist.Create;
  //IdFTP1.Host := '192.168.10.99'; //FTP服务器地址
  IdFTP1.Host := '127.0.0.1'; //FTP服务器地址
  IdFTP1.Port:= 2121; //注意端口号,这里不是默认的21,而是你自己设定的值
  IdFTP1.Username := 'my'; //FTP服务器用户名
  IdFTP1.Password := 'qwert'; //FTP服务器密码

  IdFTP1.Connect();  //连接到ftp

  edt1.Text := IdFTP1.RetrieveCurrentDir;//得到初始目录
  //IdFTP1.ChangeDir('client'); //进入到client子目录
  //IdFTP1.ChangeDir('..'); //回到上一级目录
  IdFTP1.List(tr); //得到client目录下所有文件列表
  mmo1.Lines.Assign(tr);
  tr.Free;
end;
 


procedure TForm1.btn2Click(Sender: TObject);
var
  tt :TIdFTPListItems;
  t : TIdFTPListItem;
  i : integer;
  tfname : String;
begin  //下载
  //Label1.Caption := IdFTP1.DirectoryListing.Items[0].FileName;
  IdFTP1.TransferType := ftBinary; //指定为二进制文件  或文本文件ftASCII
  for i:=0 to IdFTP1.DirectoryListing.Count-1 do
  begin
    tt := IdFTP1.DirectoryListing; //得到当前目录下文件及目录列表
    t := tt.Items[i]; //得到一个文件相关信息
   // Label1.Caption :=t.Text;  //取出一个文件信息内容
    tfname := t.FileName;
   // showmessage(t.OwnerName+'  '+t.GroupName+'  '+t.FileName+'   '+t.LinkedItemName);
    if IdFTP1.DirectoryListing.Items[i].ItemType = ditFile then //如果是文件
    begin
      IdFTP1.Get(tfname,ExtractFilePath(Application.ExeName)+tfname,True,True); //下载到本地,并为覆盖,且支持断点续传
    end;
  end;
end;

procedure TForm1.btn3Click(Sender: TObject);
var
  fi : string;
begin  //上传
  if dlgOpen1.Execute then
  begin
     fi := dlgOpen1.FileName;
    //IdFTP1.Put('f:/测试文档样本.rar','测试文档样本.rar');//上传,
      IdFTP1.Put(fi,ExtractFileName(fi));
  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('文件传送完毕。');
    ftpReady: showmessage('准备传送文件....');
    ftpAborted: showmessage('传送失败');
  end;         }

  //showmessage(AStatusText);
end;
 


end.
posted @ 2020-01-09 08:32  创新创造学习整合套路  阅读(2865)  评论(0编辑  收藏  举报