admin

[网络类] idhttp 下载 添加进度条显示

function TFrAutoUpdate.DownLoadFile(sURL, sFName: string): boolean;

var

  tStream: TMemoryStream;

begin

  Result := False;

  tStream := TMemoryStream.Create;

  try { 防止不可预料错误发生 }

    sURL := IdHTTP1.URL.URLEncode(sURL); { 下载路径进行编码转换 }

    try

      IdHTTP1.Get(sURL, tStream); { 保存到内存流 }

      tStream.SaveToFile(sFName); { 保存为文件 }

      Result := True;

    except

    end;

  finally { 即使发生不可预料的错误也可以释放资源 }

    tStream.Free;

  end;

end;




处理3个事件
OnWorkBegin AWorkCountMax则为下载的总字节数
OnWork AWorkCount则为已下载的字节数
OnWorkEnd 下载结束

进度条最大值设置为100,在OnWorkBegin中记录总字节数,在OnWork中用 AWorkCount/总字节数*100取整后设置给进度条。 


procedure TUpdatefrm.IdHTTP1WorkEnd(Sender: TObject; AWorkMode: TWorkMode);
begin
  ProgressBar1.Position := ProgressBar1.Max;
end;

procedure TUpdatefrm.IdHTTP1Work(Sender: TObject; AWorkMode: TWorkMode;
  const AWorkCount: Integer);
begin
  ProgressBar1.Position := AWorkCount;
end;

procedure TUpdatefrm.IdHTTP1WorkBegin(Sender: TObject; AWorkMode: TWorkMode;
  const AWorkCountMax: Integer);
begin
  ProgressBar1.Max := AWorkCountMax;
  ProgressBar1.Min := 0;
  ProgressBar1.Position := 0;
end; 





procedure TfrmMain.IdHTTP1WorkBegin(Sender: TObject; AWorkMode: TWorkMode;

  const AWorkCountMax: Integer);

begin

  ProgressBar1.Max:=AWorkCountMax;

  ProgressBar1.Min:=0;

  ProgressBar1.Position:=0;

end;


procedure TfrmMain.IdHTTP1Work(Sender: TObject; AWorkMode: TWorkMode;

  const AWorkCount: Integer);

begin

 ProgressBar1.Position:=ProgressBar1.Position+AWorkCount;

end;



#1楼
发帖时间:5月前   |   查看数:0   |   回复数:0
游客组