使用Delphi和Indy,使用Progress事件以编程方式从Internet下载文件 [英] Download a File from internet programatically with an Progress event using Delphi and Indy

查看:160
本文介绍了使用Delphi和Indy,使用Progress事件以编程方式从Internet下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need a way to download a file from the Internet using Delphi via HTTP, Wich include an Progress event , I'm looking for a method wich 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;

这篇关于使用Delphi和Indy,使用Progress事件以编程方式从Internet下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆