转: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.

转载于:https://www.cnblogs.com/wskc/archive/2013/05/28/3104564.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值