Einzelnen Beitrag anzeigen

diso

Registriert seit: 21. Sep 2007
6 Beiträge
 
#8

Re: Indy Http Download+Fortschritt als Funktion geht nicht

  Alt 19. Nov 2008, 15:29
warum macht ihr euch das leben schwer?

idHttp.Response.ContentStream.Position gibt an wie viel bytes aktuell gedownloaded sind.

geändertes quellcode:

Delphi-Quellcode:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Progress: TProgressBar;
    SpeedStatus: TLabel;
    Status: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    IdHTTP: TIdHTTP;
    Button1: TButton;
    procedure IdHTTPWork(ASender: TObject; AWorkMode: TWorkMode;
      AWorkCount: Integer);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    BytesInsgesamt: Integer;
    FTime: Longword; //Beide Variablen werden für die kb/sec anzeige gebraucht
    FBytes: Longword; // --"--
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
function GetSizeName(const Size : int64): String;
begin
  Result := 'Fehler';
  if Size = -1 then exit;
  if Size < 1024 then
  begin
    Result := inttostr(Size)+' Byte';
    exit;
  end;
  if (1024 <= Size) and (Size < 1048576) then
  begin
    Result := floattostr((round((Size/1024)*100))/100)+' KB';
    exit;
  end;
  if (1048576 <= Size) and (Size < 1099511627776) then
  begin
    Result := floattostr((round((Size/1048576)*100))/100)+' MB';
    exit;
  end;
  if Size > 1099511627776 then
  begin
    Result := floattostr((round((Size/1099511627776)*100))/100)+' GB';
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  lStream: TFileStream;
begin
  Status.Caption := 'Prüfe Header';
  Application.ProcessMessages;
  IdHTTP.Head(Edit1.Text);
  BytesInsgesamt := IdHTTP.Response.ContentLength;
  Status.Caption := GetSizeName(BytesInsgesamt);
  Progress.Max := BytesInsgesamt;
  Application.ProcessMessages;
  
  lStream:= TFileStream.Create(Edit2.Text, fmCreate);
  
  idHTTP.Get(edit1.Text, lStream);

  lStream.Free;
  progress.Position := progress.Max;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
edit2.Text := ExtractFilePath(ParamStr(0))+'Test.Dat';
end;

procedure TForm1.IdHTTPWork(ASender: TObject; AWorkMode: TWorkMode;
  AWorkCount: Integer);
begin
  if not((GetTickCount - FTime) <= 0) then
  begin
    try
      SpeedStatus.caption := Format('%.2f KB/s', [(AWorkCount - FBytes) / (GetTickCount - FTime)]);
    except
    end;
  end;
  FTime := GetTickCount;
  FBytes := AWorkCount;
  Progress.Position := idHttp.Response.ContentStream.Position;
  Application.ProcessMessages;
end;

end.
  Mit Zitat antworten Zitat