Indy FTP无法正确加载进度条

时间:2013-07-05 23:45:56

标签: delphi

我的一位朋友向我求助,因为他想要一个表示上传/下载某些文本文件的进度条。在这里你可以看到我使用的代码:

procedure TForm1.IdFTP1Work(ASender: TObject; AWorkMode: TWorkMode;
  AWorkCount: Int64);
begin
 Application.ProcessMessages;
 ProgressBar1.Position:= AWorkCount;
 Label6.Caption:=IntToStr(ProgressBar1.Position);
end;

procedure TForm1.IdFTP1WorkBegin(ASender: TObject; AWorkMode: TWorkMode;
  AWorkCountMax: Int64);
begin
 if(AWorkCountMax>0) then
  begin
    ProgressBar1.Max:=AWorkCountMax;
  end;
  ProgressBar1.Position:=0;
end;

procedure TForm1.IdFTP1WorkEnd(ASender: TObject; AWorkMode: TWorkMode);
begin
 ProgressBar1.Position:=0;
end;

在我的表格上我还有2个标签:第一个表示进展,名为Label6,另一个只有“%”标题,如下所示。 Label 6的值应该从0到100。

enter image description here

他发送的文本文件现在很小,但它们会更大。我的问题是我没有看到程序中的条形进展(绿色矩形),而且标签6上的值超过100并且到达700.我该怎么办?

1 个答案:

答案 0 :(得分:8)

AWorkCountMaxAWorkCount是字节数,而不是百分比。更新UI时需要考虑到这一点,特别是如果要显示百分比。

据说,AWorkCountMax仅对上传有效,绝不会在下载上有效。原因是因为FTP协议在传输过程中不传输文件大小,因此TIdFTP不知道正在下载的文件的大小,只知道正在上载的本地文件的大小。要下载,您必须先使用TIdFTP.Size()TIdFTP.List()TIdFTP.ExtListItem()手动检索文件大小。

完全远离Application.ProcessMessages()。如果要触发重绘,请改用Update()

试试这个:

procedure TForm1.IdFTP1WorkBegin(ASender: TObject; AWorkMode: TWorkMode; AWorkCountMax: Int64);
begin
  if AWorkMode = wmWrite then
    ProgressBar1.Max := AWorkCountMax
  else;
    ProgressBar1.Max := ...; // value retrieved beforehand...
  ProgressBar1.Position := 0;
end;

procedure TForm1.IdFTP1Work(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
begin
  if ProgressBar1.Max > 0 then
  begin
    ProgressBar1.Position := AWorkCount;
    Label6.Caption := IntToStr((ProgressBar1.Position * 100) div ProgressBar1.Max) + '%';
  end else
    Label6.Caption := IntToStr(AWorkCount) + ' bytes';
  Update;
end;

procedure TForm1.IdFTP1WorkEnd(ASender: TObject; AWorkMode: TWorkMode);
begin
  ProgressBar1.Position := 0;
end;

可替换地:

var
  WorkMax: Int64;

procedure TForm1.IdFTP1WorkBegin(ASender: TObject; AWorkMode: TWorkMode; AWorkCountMax: Int64);
begin
  if AWorkMode = wmWrite then
    WorkMax := AWorkCountMax
  else;
    WorkMax := := ...; // value retrieved beforehand...
  ProgressBar1.Min := 0;
  ProgressBar1.Max := 100;
  ProgressBar1.Position := 0;
end;

procedure TForm1.IdFTP1Work(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
begin
  if WorkMax > 0 then
  begin
    ProgressBar1.Position := (AWorkCount * 100) div WorkMax;
    Label6.Caption := IntToStr(ProgressBar1.Position) + '%';
  end else
    Label6.Caption := IntToStr(AWorkCount) + ' bytes';
  Update;
end;

procedure TForm1.IdFTP1WorkEnd(ASender: TObject; AWorkMode: TWorkMode);
begin
  ProgressBar1.Position := 0;
end;