Q&A

  • FTP CLIENT에서 파일 다운.업로드상태에서 속도표시 문제
환경: Indy 9.0, 델파이 7.0,
사용자:왕초보~
인디로 ftp client 구현중에 다운로드 업로드시 현재및 평균 속도표시가 이루어지도록 했는데
다운로드및 업로드만으로도 버벅거려서 다운로드및 업로드 완료후에만 표시되서요~
어디에 문제가 있는지 잘 모르겠어요~~ 부탁드립니다. ..
실행파일 첨부해볼께요~~

<!--CodeS-->
unit Unit_FtpClient;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    FTP: TIdFTP;
    StatusBar: TStatusBar;
    Msg_Debug: TListBox;
    DirectoryList: TListBox;
    SaveDialog: TSaveDialog;
    ProgressBar: TProgressBar;
    UBtn: TButton;
    OpenDialog: TOpenDialog;
    SPLabel: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Host: TEdit;
    Id: TEdit;
    Pass: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure FTPStatus(ASender: TObject; const AStatus: TIdStatus;
      const AStatusText: String);
    procedure DirectoryListDblClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FTPWork(Sender: TObject; AWorkMode: TWorkMode;
      const AWorkCount: Integer);
    procedure FTPWorkBegin(Sender: TObject; AWorkMode: TWorkMode;
      const AWorkCountMax: Integer);
    procedure UBtnClick(Sender: TObject);
  private
    STime:TDateTime;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  FileList:TStringlist;
  ASpeed:Double=0;
  PSpeed:Double=0;  
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
//var FileList:TStringlist;
begin
  FTP.Host:=Host.Text;
  FTP.Username:=Id.Text;
  FTP.Password:=Pass.Text;
  FTP.Connect;

  FTP.List(FileList);
  DirectoryList.Items.Assign(FileList);
end;

procedure TForm1.FTPStatus(ASender: TObject; const AStatus: TIdStatus;
  const AStatusText: String);
begin
  Msg_Debug.Items.Add(AStatusText);
  StatusBar.Panels[1].Text:=AStatusText;
end;

procedure TForm1.DirectoryListDblClick(Sender: TObject);
var
  Name:String;
begin
  Name:=FTP.DirectoryListing.Items[DirectoryList.ItemIndex].FileName;
  if FTP.DirectoryListing.Items[DirectoryList.ItemIndex].ItemType = ditDirectory then begin
    FTP.ChangeDir(Name);
    DirectoryList.Clear;
    FileList.Clear;
    FTP.List(FileList);
    DirectoryList.Items.Assign(FileList);
  end else if FTP.DirectoryListing.Items[DirectoryList.ItemIndex].ItemType = ditFile then begin
    SaveDialog.FileName:=Name;
    if SaveDialog.Execute then begin
      ProgressBar.Position:=0;
      ProgressBar.Max:=FTP.Size(Name);
      FTP.TransferType:=ftBinary;
      FTP.Get(Name,SaveDialog.FileName,true);
    end;
  end;
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
    FileList:=TStringlist.Create;
    ProgressBar.Parent:=StatusBar;
    ProgressBar.Left:=0;
    ProgressBar.Top:=2;

end;

procedure TForm1.FTPWork(Sender: TObject; AWorkMode: TWorkMode;
  const AWorkCount: Integer);
var
  SPTime:TDateTime;
  Hour, Min, Sec, MSec:Word;
  SPSec:Double;
  _ASpeed,_PSpeed,SPTitle:String;
begin
  SPTime:=Now-STime;
  DecodeTime(SPTime, Hour, Min, Sec, MSec);
  SPSec:=Hour*60*60+Min*60+Sec+MSec/1000;
  if SPSec>0 then begin //SPSec 0일경우 0으로 나눈다는 오류메세지땜에
    ASpeed:=(ASpeed+(AWorkCount/1024)/SPSec)/2;
    PSpeed:=(AWorkCount/1024)/SPSec;
  end;
  if ASpeed>0 then begin
    _ASpeed:=FormatFloat('0.00 KB/S',ASpeed);
    _PSpeed:=FormatFloat('0.00 KB/S',PSpeed);    
  end;
  case AWorkMode of
    wmRead: SPTitle:='Download';
    wmWrite: SPTitle:='Upload';
  end;

  //StatusBar.Panels[1].Text:=SPTitle+' 평균속도: '+_ASpeed+' 현재속도: '+_PSpeed;
  SPLabel.Caption:=SPTitle+' 평균속도: '+_ASpeed+' 현재속도: '+_PSpeed;

  //ShowMessage('AWorkCount'+IntToStr(AWorkCount));
  ProgressBar.Position:=AWorkCount;
end;

procedure TForm1.FTPWorkBegin(Sender: TObject; AWorkMode: TWorkMode;
  const AWorkCountMax: Integer);
begin
  STime:=Now;
end;

procedure TForm1.UBtnClick(Sender: TObject);
var
    F:TextFile;
    FSize:Longint;
begin
  if OpenDialog.Execute then begin
    ProgressBar.Position:=0;
    AssignFile(F,OpenDialog.FileName);
    Reset(F);    
    FSize:=FileSize(F)*128;
    ProgressBar.Max:=FSize;
    FTP.TransferType:=ftBinary;
    FTP.Put(OpenDialog.FileName,ExtractFileName(OpenDialog.FileName));
    DirectoryList.Clear;
    FileList.Clear;
    FTP.List(FileList);
    DirectoryList.Items.Assign(FileList);
  end;
end;

end.
<!--CodeE-->


1  COMMENTS