unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, IdHTTP, StdCtrls;
const
CFileName = 'D:/Tank12.html';
CUrl = 'http://game.yninfo.com/file/C1/20060824/115638460263481700.html';
CBlock = 1024;
type
TForm1 = class;
TDownThread = class(TThread)
private
FOwner: TForm1;
protected
procedure Execute; override;
public
constructor Create(AOwner: TForm1);
end;
TForm1 = class(TForm)
btnPause: TButton;
Edit1: TEdit;
IdHTTP1: TIdHTTP;
btnDownload: TButton;
ProgressBar1: TProgressBar;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure btnDownloadClick(Sender: TObject);
procedure btnPauseClick(Sender: TObject);
procedure IdHTTP1WorkEnd(Sender: TObject; AWorkMode: TWorkMode);
private
FThread: TDownThread;
FFile: TFileStream;
FFileSize: Integer;
{ Private declarations }
protected
procedure Execute;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TDownThread }
constructor TDownThread.Create(AOwner: TForm1);
begin
inherited Create(True);
FOwner := AOwner;
end;
procedure TDownThread.Execute;
begin
while not Terminated do
FOwner.Execute;
end;
{ TForm1 }
procedure TForm1.Execute;
begin
FFile.Position := FFile.Size;
IdHttp1.Request.ContentRangeStart := FFile.Position;
IdHttp1.Request.ContentRangeEnd := IdHttp1.Request.ContentRangeStart + CBlock;
if IdHttp1.Request.ContentRangeEnd > FFileSize then
IdHttp1.Request.ContentRangeEnd := FFileSize;
IdHttp1.Get(CUrl, FFile);
if FFile.Size = FFileSize then
FThread.Terminate;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
if FileExists(CFileName) then
FFile := TFileStream.Create(CFileName, fmOpenWrite)
else
FFile := TFileStream.Create(CFileName, fmCreate);
IdHttp1.Head(CUrl);
FFileSize := IdHttp1.Response.ContentLength;
ProgressBar1.Min := 0;
ProgressBar1.Max := FFileSize;
ProgressBar1.Position := FFile.Size;
FThread := TDownThread.Create(Self);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
FThread.Free;
FFile.Free;
end;
procedure TForm1.btnDownloadClick(Sender: TObject);
begin
FThread.Resume;
end;
procedure TForm1.btnPauseClick(Sender: TObject);
begin
FThread.Suspend;
end;
procedure TForm1.IdHTTP1WorkEnd(Sender: TObject; AWorkMode: TWorkMode);
begin
ProgressBar1.Position := FFile.Position;
end;
end.