现在我在利用FTPClient组件进行上传下载,但是我想显示上传下载的进度,不知道在那个事件中可以做到,请各位指点.

解决方案 »

  1.   

    大概的一个下载线程:
    unit UThread;interfaceuses
      Classes,IdFTP,windows,sysUtils,IdComponent,StdCtrls;type
      TDownThread = class(TThread)
      private
        { Private declarations }
        FServerIp : string;
        FPort : integer;
        fUserName : String;
        fPwd : String;
        fFileName : String;
        fDir : String;
        fAllSize : Int64;
        fCurrentSize : int64;
        fSavePath : String;
        fLines : integer;    ftpClient : TIDFTP;    IsDown : boolean;
        procedure FtpWork(Sender: TObject; AWorkMode: TWorkMode;
                            const AWorkCount: Integer);
        procedure FtpWorkEnd(Sender: TObject; AWorkMode: TWorkMode);
        procedure FtpWorkBegin(Sender: TObject; AWorkMode: TWorkMode;const AWorkCountMax: Integer);
          protected
        procedure Execute; override;  public
        property ServerIp : String read FServerIp write FServerIp;
        property Port : integer read FPort write FPort;
        property UserName : String read FUserName write FUserName;
        property Pwd : String read FPwd write FPwd;
        property FileName : String read FFileName write fFileName;
        property Dir : String read fDir write fDir;
        property AllSize : Int64 read fAllSize write fAllSize;
        property CurrentSize : int64 read fCurrentSize write fCurrentSize;
        property SavePath : String read fSavePath write fSavePath;
        property Lines : integer read fLines write fLines;
        Procedure BeginDown;
      end;implementationuses UMain;{ Important: Methods and properties of objects in visual components can only be
      used in a method called using Synchronize, for example,      Synchronize(UpdateCaption);  and UpdateCaption could look like,    procedure TDownThread.UpdateCaption;
        begin
          Form1.Caption := 'Updated in a thread';
        end; }{ TDownThread }procedure TDownThread.BeginDown;
    begin
      //Self.Execute;
      Self.Resume;
    end;
    procedure TDownThread.Execute;
    begin
      FreeOnTerminate := True;  IsDown := False;  ftpClient := TIdFTP.Create(nil);  try
        ftpClient.RecvBufferSize := 2048;
        ftpClient.SendBufferSize := 2048;    ftpClient.Host := FServerIp;
        ftpClient.Port := FPort;
        ftpClient.Username := fUserName;
        ftpClient.Password := fPwd;    ftpClient.OnWorkBegin := FtpWorkBegin;
        ftpClient.OnWork := ftpwork;
        ftpClient.OnWorkEnd := FtpWorkEnd;    ftpClient.Connect(True,8000);    ftpClient.ChangeDir(fDir);
        IsDown := True;
        ftpClient.Get(FileName,SavePath+'\'+FileName,True);
       // ftpClient.Put();  //上传
      finally
        ftpClient.Free;
      end;
    end;procedure TDownThread.FtpWork(Sender: TObject; AWorkMode: TWorkMode;
      const AWorkCount: Integer);
    begin
      if (AWorkMode = wmRead) and (IsDown = true) then
      begin
         FCurrentSize := AWorkCount;     frm_Main.myLabel[fLines].Caption := fFileName + '  下载进度:'+IntToStr(AWorkCount div 1024 )+'K/'+IntToStr(AllSize div 1024)+'K';
      end;
    end;procedure TDownThread.FtpWorkBegin(Sender: TObject; AWorkMode: TWorkMode;
      const AWorkCountMax: Integer);
    begin
      //
    end;  procedure TDownThread.FtpWorkEnd(Sender: TObject; AWorkMode: TWorkMode);
    begin
      if (AWorkMode = wmRead) and (IsDown = true) then
      begin
        if (fCurrentSize + 2048) >= fAllSize then
        begin
          frm_Main.myLabel[fLines].Caption := fFileName+ '  下载完毕';
          frm_Main.DownCount := frm_Main.DownCount + 1;
          IsDown := False;
        end
        else
        begin
          frm_Main.myLabel[fLines].Caption := fFileName+ '  下载意外终端,请重新下载...';
          frm_Main.DownCount := frm_Main.DownCount + 1;
          IsDown := False;
        end;
      end;
    end;end.
      

  2.   

    frm_Main.myLabel[fLines].Caption := fFileName + '  下载进度:'+IntToStr(AWorkCount div 1024 )+'K/'+IntToStr(AllSize div 1024)+'K';
    这个进度并不是实际的进度,我的意思是这个文件多大,已经上传下载了多少,是一个百分比.
      

  3.   

    晕,我上面的就是显示实际下载了多少,
    你将AWorkCount div 1024  除以 AllSize div 1024 乘以100不久知道了吗
      

  4.   

    那请问在WorkBegin事件中FTPClientWorkBegin(Sender: TObject;
      AWorkMode: TWorkMode; const AWorkCountMax: Integer);AWorkCountMax,这个值代表什么意思咯,谢谢.
      

  5.   

    理论上相当于allsize  不过由于存在多线程分割下载情况,也就是表示该下载线程要下载的总大小
      

  6.   

    我想得到下载的速度,是下载多少KB/S,这个怎么实现咯