gpt4 book ai didi

delphi - 使用 Delphi 和 Indy 通过 Progress 事件以编程方式从互联网下载文件

转载 作者:行者123 更新时间:2023-12-03 14:36:18 33 4
gpt4 key购买 nike

我需要一种使用 Delphi 通过 HTTP 从 Internet 下载文件的方法,其中包括 Progress 事件,我正在寻找一种使用 Indy 组件的方法。

我使用的是 Delphi 7。

最佳答案

我已经使用 Indy 10 编写了这个示例,仅使用一个 HTTP GET,希望它也适用于 Indy 9:

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;

关于delphi - 使用 Delphi 和 Indy 通过 Progress 事件以编程方式从互联网下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2184473/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com