Display progress

Hi,


is there a way to retrieve how many bytes have already been downloaded to display the progress of the GET call?

Kind regards
gernot

You should get the content as stream using THttpResponse.ContentAsStream method. THttpClient.Send method returns immediately and then as you read from the stream the file is downloaded, so you have total flexibility over how to display the progress. Rough example:


procedure TForm1.Button1Click(Sender: TObject);
const
  BufSize = 65536;
var
  Buf: array[0..BufSize - 1] of byte;
  c: THttpClient;
  req: THttpRequest;
  resp: THttpResponse;
  f: TFileStream;
  s: TStream;
  BytesRead: integer;
  TotalRead: integer;
begin
  memo1.Lines.Clear;
  c := THttpClient.Create;
  req := c.CreateRequest;
  req.Uri := SomeUrl;
  resp := c.Send(req);
  f := TFileStream.Create('D:\downloadedfile.dat', fmCreate);
  s := resp.ContentAsStream;
  TotalRead := 0;
  repeat
    BytesRead := s.Read(Buf[0], BufSize);
    f.Write(Buf[0], BytesRead);
    TotalRead := TotalRead + BytesRead;
    Label1.Caption := Format('%d Kb read', [TotalRead div 1024]);
    Application.ProcessMessages;
  until BytesRead < BufSize;
  F.Free;
  req.Free;
  resp.Free;
end;