使用Delphi和Indy以编程方式从Internet下载带有Progress事件的文件
问题描述:
我需要一种使用Delphi通过HTTP从Internet上下载文件的方法,
其中包括一个Progress事件,我正在寻找一种使用Indy组件的方法。
I need a way to download a file from the Internet using Delphi via HTTP, Which include an Progress event, I'm looking for a method which uses the Indy components.
我正在使用Delphi 7。
I am using Delphi 7.
答
我已经使用一个HTTP GET对该示例进行了编码,对于Indy 10,希望也能与Indy 9一起使用。
I've coded this example, using just one HTTP GET, with Indy 10, hope it works with Indy 9 too:
uses
{...} IdHTTP, IdComponent;
type
TFormMain = class(TForm)
{...}
private
{...}
procedure HttpWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
end;
{...}
procedure TFormMain.Button1Click(Sender: TObject);
var
Http: TIdHTTP;
MS: TMemoryStream;
begin
Http := TIdHTTP.Create(nil);
try
MS := TMemoryStream.Create;
try
Http.OnWork:= HttpWork;
Http.Get('http://live.sysinternals.com/ADExplorer.exe', MS);
MS.SaveToFile('C:\ADExplorer.exe');
finally
MS.Free;
end;
finally
Http.Free;
end;
end;
procedure TFormMain.HttpWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
var
Http: TIdHTTP;
ContentLength: Int64;
Percent: Integer;
begin
Http := TIdHTTP(ASender);
ContentLength := Http.Response.ContentLength;
if (Pos('chunked', LowerCase(Http.Response.TransferEncoding)) = 0) and
(ContentLength > 0) then
begin
Percent := 100*AWorkCount div ContentLength;
MemoOutput.Lines.Add(IntToStr(Percent));
end;
end;