Home > Back-end >  IdHTTP download file, how to add a progress bar?
IdHTTP download file, how to add a progress bar?

Time:09-19

How to add a ProgressBar progress bar showing progress?
 function TFrAutoUpdate. DownLoadFile (sURL, sFName: string) : Boolean; 
Var
TStream: TMemoryStream;
The begin
Result :=False;
TStream:=TMemoryStream. Create;
Try {to prevent unexpected errors occur}
SURL:=IdHTTP1. URL. URLEncode (sURL); {download code path conversion}
Try
IdHTTP1. Get (sURL, tStream); {save the memory stream}
TStream. SaveToFile (sFName); {is saved as a file}
Result :=True;
Except,
end;
The finally {even unexpected error can release resources}
TStream. Free;
end;
end;

CodePudding user response:

IdHTTP event OnWork write pb1. Position:=AWorkCount
OnWorkBegin write pb1. Max:=AWorkCountMax;

Pb1. Position:=0;

We have to do is

CodePudding user response:

To deal with three events
OnWorkBegin AWorkCountMax is the total number of bytes for download
OnWork AWorkCount was of the number of bytes downloaded
Download OnWorkEnd end

A maximum progress bar set to 100, record the total number of bytes in the OnWorkBegin, use AWorkCount/total number of bytes in OnWork * 100 integer after setting for the progress bar,

CodePudding user response:

Progress with the upstairs..

In addition, if just download file, more simple can be directly call API function. UrlDownloadToFile

CodePudding user response:

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

Procedure TUpdatefrm. IdHTTP1Work (Sender: TObject; AWorkMode: TWorkMode;
Const AWorkCount: Integer);
The begin
ProgressBar1. Position:=AWorkCount;
end;

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

CodePudding user response:

 
Procedure TfrmMain. IdHTTP1WorkBegin (Sender: TObject; AWorkMode: TWorkMode;
Const AWorkCountMax: Integer);
The begin
ProgressBar1. Max:=AWorkCountMax;
ProgressBar1. Min:=0;
ProgressBar1. Position:=0;
end;

Procedure TfrmMain. IdHTTP1Work (Sender: TObject; AWorkMode: TWorkMode;
Const AWorkCount: Integer);
The begin
ProgressBar1. Position:=ProgressBar1. Position + AWorkCount;
end;

  • Related