Delphi - 使用Synapse下载带有进度的文件

时间:2013-05-02 19:00:05

标签: delphi http download synapse

我一直在使用Synapse一段时间,主要是发送电子邮件。今天我正在创建一个简单的安装程序,并尝试通过HTTP下载应用程序exe文件。该文件大小约为9 MB,所以我想向用户添加进度状态,但我不明白我找到的例子。这是我到目前为止所得到的:

type
  THookSocketStatus = Procedure(Sender: TObject; Reason: THookSocketReason; const Value: String) of Object;
  CallBack = class
    Class Procedure Status(Sender: TObject; Reason: THookSocketReason; const Value: String);
  end;


Class Procedure CallBack.Status(Sender: TObject; Reason: THookSocketReason; const Value: String);
var
  V: String;
Begin
  V := GetEnumName(TypeInfo(THookSocketReason), Integer(Reason)) + ' ' + Value;
  Form1.mem1.Lines.Add(V);
  application.ProcessMessages;
end;

procedure TForm1.btn1Click(Sender: TObject);
var
  HTTP: THTTPSend;
  MSTM: TMemoryStream;
begin
  Screen.Cursor := crHourGlass;
  HTTP := THTTPSend.Create;
  MSTM := TMemoryStream.Create;
  Try
    Try
      HTTP.Sock.OnStatus := CallBack.Status;
      If HTTP.HTTPMethod('GET', edt1.Text) Then
      Begin
        MSTM.Seek(0, soFromBeginning);
        MSTM.CopyFrom(HTTP.Document, 0);
        MSTM.SaveToFile(ExtractFilePath(Application.ExeName) + 'test.exe');
      end;
    Except
    end;
  Finally
    MSTM.Free;
    HTTP.Free;
    Screen.Cursor := crDefault;
  end;
end;

在这个简单的测试中我得到了这个结果:

HR_SocketClose
HR_ResolvingBegin www.website.com:80
HR_ResolvingEnd 176.102.295.18:80
HR_SocketCreate IPv4
HR_Connect www.website.com:80
HR_WriteCount 158
HR_CanRead
HR_ReadCount 288
HR_CanRead
HR_ReadCount 8192
HR_ReadCount 8192
HR_ReadCount 8192
HR_ReadCount 6720
HR_CanRead
HR_ReadCount 3299
.
.
.
HR_ReadCount 8192
HR_ReadCount 8192
HR_ReadCount 7828
HR_SocketClose
HR_SocketClose

请问,WriteCount和ReadCount是什么意思?如何在开始下载之前获取总文件大小以设置进度条?

谢谢你们!

0 个答案:

没有答案