当前位置:首页 > 网站技术文档 > 正文内容

Delphi NetHTTPClient1 下载显示进度条

nalyc1个月前 (09-17)网站技术文档134
unit untFrmDownFile;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.Net.URLClient,
  System.Net.HttpClient, System.Net.HttpClientComponent, Vcl.ComCtrls,
  Vcl.StdCtrls;

type
  TFrmDown = class(TForm)
    NetHTTPClient1: TNetHTTPClient;
    ProgressBar1: TProgressBar;
    Label1: TLabel;
    procedure NetHTTPClient1RequestCompleted(const Sender: TObject;
      const AResponse: IHTTPResponse);
    procedure NetHTTPClient1ReceiveData(const Sender: TObject; AContentLength,
      AReadCount: Int64; var Abort: Boolean);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
    AResponse: TStringStream;
  public
    { Public declarations }
    FileName: string;

    procedure DoDownload(Url: string);
  end;

var
  FrmDown: TFrmDown;

implementation

{$R *.dfm}

//delphi 字节数转换为KB或MB字符串
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;

procedure TFrmDown.DoDownload(Url: string);
begin
  AResponse.Clear;
  NetHTTPClient1.Get(Url,AResponse);
end;

procedure TFrmDown.FormCreate(Sender: TObject);
begin
  AResponse := TStringStream.Create('',TEncoding.UTF8);
end;

procedure TFrmDown.FormDestroy(Sender: TObject);
begin
  FreeAndNil(AResponse);
end;

procedure TFrmDown.FormShow(Sender: TObject);
begin
  ProgressBar1.Position := 0;
end;

procedure TFrmDown.NetHTTPClient1ReceiveData(const Sender: TObject;
  AContentLength, AReadCount: Int64; var Abort: Boolean);
begin
  ProgressBar1.Max := AContentLength;
  ProgressBar1.Position := AReadCount;
  Label1.Caption := BytesToStr(AReadCount) + ' / ' + BytesToStr(AContentLength);
end;

procedure TFrmDown.NetHTTPClient1RequestCompleted(const Sender: TObject;
  const AResponse: IHTTPResponse);
begin
  try
    if AResponse.ContentStream.Size > 0  then
    begin
      if FileExists(FileName) then
        DeleteFile(FileName);
      Self.AResponse.SaveToFile(FileName);
      Application.MessageBox('下载成功', '提示信息', MB_OK + MB_ICONINFORMATION);
      Self.Close;
    end;
  except
    Self.Close;
  end;
end;

end.

扫描二维码推送至手机访问。

版权声明:本文由Qzcc.Com发布,如需转载请注明出处。

本文链接:http://qzcc.com/post/Delphi_NetHTTPClient.html

分享给朋友:

相关文章

Steema TeeChart Pro VCL FMX Source Code 2020.30 delphi10.4.2编译出错:E2066、E2052、E2280

Steema TeeChart Pro VCL FMX Source Code 2020.30 delphi10.4.2编译出错:E2066、E2052、E2280

VCLTee.TeeConst.pas(612) Error: E2066 Missing operator or semicolon VCLTee.TeeConst.pas(612) Error: E2066 Missing operat...

delphi XE10开发的APP对于苹果IPV6上架要求的处理(DelphiTeacher)

1.服务器必须使用域名.不能使用IP地址2.Indy的话,域名加[]3.DataSnap的话,Params.Values['CommunicationIPVersion'] :='IP_IPv6';4.当前...

delphi FireDAC FDConnection 事务

//示例代码 begin     FDConnection1.StartTransaction; //开始一个事务    try  &...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。