3
我一直在使用Synapse一段時間,主要發送電子郵件。今天我正在創建一個簡單的安裝程序,並嘗試通過HTTP下載應用程序exe文件。該文件大小約爲9 MB,因此我想向用戶添加進度狀態,但我不瞭解我找到的示例。以下是我走到這一步:德爾福 - 使用Synapse下載包含進度的文件
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?如何在開始下載之前獲取總文件大小以設置進度欄?
謝謝你們!
我相信字節數讀取或寫入。如果內容不是文本,則應該能夠訪問從GET請求返回的標題,然後查找「內容長度:3495」中的「內容長度」並獲取文件總大小。不幸的是,我從來沒有使用Synapse,所以我不能提供更多的指導。 – Glenn1234 2013-05-02 21:28:15
嗨@ Glenn1234,謝謝。我將嘗試獲取有關如何獲取GET標題的更多信息。 – Guybrush 2013-05-02 21:47:04
好吧,我放棄了試圖用Synapse做到這一點。我再次使用Indy,這樣做更容易。不管怎樣,謝謝你! – Guybrush 2013-05-03 15:28:19