转:delphi用URLDownloadToFile下载文件,用进度条跟踪下载进度
用URLDownloadToFile下载文件,如何用进度条跟踪下载进度
1:OnDownloadProgress
2:可有否具体的例子。
3:unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtActns, ComCtrls;
type
TForm1 = class(TForm)
ProgressBar1: TProgressBar;
Label1: TLabel;
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
DownLoadURL : TDownLoadURL;
procedure DownloadProgress(Sender: TDownLoadURL; Progress,
ProgressMax: Cardinal; StatusCode: TURLDownloadStatus; StatusText: String;
var Cancel: Boolean);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.DownloadProgress(Sender: TDownLoadURL; Progress,
ProgressMax: Cardinal; StatusCode: TURLDownloadStatus;
StatusText: String; var Cancel: Boolean);
begin
Label1.Caption := StatusText;
ProgressBar1.Max := ProgressMax;
ProgressBar1.Position := Progress;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
DownLoadURL := TDownLoadURL.Create(self);
with DownLoadURL do
begin
FileName := 'c:\p.exe';
URL := 'http://www.teechart.net/files/vcl/public/TeeChartPro6/TeeChart601Delphi6_RTL3.EXE';
OnDownloadProgress := DownloadProgress;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
DownLoadURL.ExecuteTarget(DownLoadURL);
end;
end.
4:谢谢!
5:用我给你提供的类吧:
unit unt_Class_TDownLoad;
interface
uses
ExtActns,ComCtrls;
{ TDownLoad }
type
TDownLoad = class(TObject)
private
FDownLoadFileURL:string;
FLocalFileName:string;
FProgressBar:TProgressBar;
FDownLoadURL : TDownLoadURL;
protected
procedure OnDownloadProgress(Sender: TDownLoadURL; Progress,
ProgressMax: Cardinal; StatusCode: TURLDownloadStatus;
StatusText: String; var Cancel: Boolean);
public
property DownLoadFileURL:string read FDownLoadFileURL write FDownLoadFileURL;
property LocalFileName:string read FLocalFileName write FLocalFileName;
property ProgressBar:TProgressBar read FProgressBar write FProgressBar;
//constructor Create;
constructor Create(ProgressBar:TProgressBar);
function DownLoad():Boolean;overload;
function DownLoad(Url,LocalFilename:string):Boolean;overload;
destructor Destroy; override;
published
end;
implementation
constructor TDownLoad.Create(ProgressBar:TProgressBar);
begin
inherited Create();
FProgressBar:=ProgressBar;
FDownLoadURL:= TDownLoadURL.Create(nil);
FDownLoadURL.OnDownloadProgress:=OnDownloadProgress;
end;
procedure TDownLoad.OnDownloadProgress(Sender: TDownLoadURL; Progress,
ProgressMax: Cardinal; StatusCode: TURLDownloadStatus;
StatusText: String; var Cancel: Boolean);
begin
FProgressBar.Max := ProgressMax;
FProgressBar.Position := Progress;
end;
function TDownLoad.DownLoad():Boolean;
begin
Result:=false;
if (FDownLoadFileURL='') or (FLocalFileName='') then
//RaiseFieldNotFound();
exit;
try
with FDownLoadURL do
begin
FileName := FLocalFileName;
URL := FDownLoadFileURL;
ExecuteTarget(FDownLoadURL);
end;
except
//
end;
Result:=True;
end;
function TDownLoad.DownLoad(Url,LocalFilename:string):Boolean;
begin
Result:=False;
FDownLoadFileURL:=Url;
FLocalFileName:=LocalFilename;
try
with FDownLoadURL do
begin
FileName := FLocalFileName;
URL := FDownLoadFileURL;
ExecuteTarget(FDownLoadURL);
end;
except
//
end;
Result:=True;
end;
destructor TDownLoad.Destroy;
begin
//
end;
end.