unit Unit1;
interface
uses
//引用 Vcl.ExtActns
Vcl.ExtActns,
System.SysUtils, System.Types, System.UITypes, System.Classes,
System.Variants,
FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls,
IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, FMX.Edit;
type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
ProgressBar1: TProgressBar;
Edit1: TEdit;
GroupBox2: TGroupBox;
Edit3: TEdit;
Edit4: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
procedure URL_OnDownloadProgress(Sender: TDownLoadURL;
Progress, ProgressMax: Cardinal; StatusCode: TURLDownloadStatus;
StatusText: String; var Cancel: Boolean);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.fmx}
var
DownLoadURL1:TDownLoadURL;
//url=网络文件 'http://helloroman.oicp.net:8000/test.rar';
//Filename=保存到本地文件 'D:\Administrator\Desktop\123.rar';
function DownLoadFile(url,Filename:string):boolean;
var
DownLoadURL1:TDownLoadURL;
begin
try
DownLoadURL1:=TDownLoadURL.Create(Form1);
DownLoadURL1.URL:= url;
DownLoadURL1.Filename:= Filename;
DownLoadURL1.OnDownloadProgress:=Form1.URL_OnDownloadProgress;
DownLoadURL1.ExecuteTarget(nil);
DownLoadURL1.Free;
Result:=true;
except
Result:=false;
end;
end;
procedure DownLoadThread;
begin
Form1.label3.Text:='0 kb / 0 kb';
if DirectoryExists(ExtractFilePath(Form1.edit4.text)) then
begin
if not DownLoadFile(Form1.edit3.text,Form1.edit4.text) then
Form1.GroupBox1.Text:='下载失败'
else
Form1.GroupBox1.Text:='下载完毕';
end
else
SHowMessage(Form1.edit4.text + '不存在!');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
TThread.CreateAnonymousThread(DownLoadThread).Start;
end;
function BytesToStr(iBytes: Integer): String;
var
iKb: Integer;
begin
iKb := Round(iBytes / 1024);
if iKb > 1000 then
Result := Format('%.2f MB', [iKb / 1024])
else
Result := Format('%d KB', [iKb]);
end;
// 获取网络文件名
function GetUrlFileName(url:string):string;
var
str:string;
begin
url:=StringReplace(StrRScan(PChar(url),'/'), '/', '',[rfReplaceAll]);
if Pos('=',url) > 0 then
url:=StringReplace(StrRScan(PChar(url),'='), '=', '',[rfReplaceAll]);
Result:=url;
end;
procedure TForm1.URL_OnDownloadProgress(Sender: TDownLoadURL;
Progress, ProgressMax: Cardinal; StatusCode: TURLDownloadStatus;
StatusText: String; var Cancel: Boolean);
begin
ProgressBar1.Max := ProgressMax div 100;
ProgressBar1.Value := Progress div 100;
Caption := StatusText;
case StatusCode of
dsFindingResource:GroupBox1.Text:='查找资源...';
dsConnecting:GroupBox1.Text:='连接中...';
dsRedirecting:GroupBox1.Text:='';
dsBeginDownloadData:GroupBox1.Text:='准备下载文件...';
dsDownloadingData:GroupBox1.Text:='下载中...';
end;
Edit1.Text:= Format('文件名:%s',[GetUrlFileName(Edit3.Text)]);
label3.Text := Format('%s / %s', [BytesToStr(Progress),BytesToStr(ProgressMax)]);
end;
end.
http://www.cnblogs.com/xe2011/p/3719454.html