idTCP檔案傳輸 如何知道下載的進度 |
答題得分者是:dennieschang
|
ddddd688
一般會員 發表:8 回覆:8 積分:3 註冊:2007-01-30 發送簡訊給我 |
Client端
<textarea class="delphi" rows="10" cols="60" name="code">unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, ComCtrls; type TForm1 = class(TForm) IdTCPClient1: TIdTCPClient; Button1: TButton; ProgressBar1: TProgressBar; OpenDialog1: TOpenDialog; Edit1: TEdit; Button2: TButton; StatusBar1: TStatusBar; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var ASize: Integer; AFileStream: TFileStream; begin IdTCPClient1.Host:='127.0.0.1'; IdTCPClient1.Port:=8514; IdTCPClient1.Connect; AFileStream:=TFileStream.Create(edit1.Text, fmOpenRead); IdTCPClient1.WriteLn(Format('%d|%s', [AFileStream.Size, ExtractFileName(Edit1.Text)])); IdTCPClient1.OpenWriteBuffer; IdTCPClient1.WriteStream(AFileStream, true, false, AFileStream.Size); IdTCPClient1.CloseWriteBuffer; AFileStream.Free; showmessage('傳送完畢!'); IdTCPClient1.Disconnect; end; procedure TForm1.Button2Click(Sender: TObject); begin if opendialog1.Execute then edit1.Text:=opendialog1.Filename; end; end.</textarea> Server端 <textarea class="delphi" rows="10" cols="60" name="code">unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, IdBaseComponent, IdComponent, IdTCPServer,math, ComCtrls, ExtCtrls, Gauges; type TForm1 = class(TForm) IdTCPServer1: TIdTCPServer; SaveDialog1: TSaveDialog; StatusBar1: TStatusBar; procedure idtcpserver1onexecute(AThread: TIdPeerThread); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.idtcpserver1onexecute(AThread: TIdPeerThread); var filename_size:string; filename:string; AFileStream: TFileStream; filesize:integer ; Asize:int64; begin with AThread.Connection do begin filename_size:= AThread.Connection.ReadLn(); filename:=Copy(filename_size, Pos('|', filename_size) 1, Length(filename_size)); filesize:=StrToInt(Copy(filename_size, 0, Pos('|', filename_size) - 1)); AFileStream:=TFileStream.Create('c:\test\' filename, fmCreate); ASize := Min(ASize - AFileStream.Size, AThread.Connection.RecvBufferSize); AThread.Connection.ReadStream(AFileStream, asize); Disconnect; AFileStream.Free; end; end; end.</textarea> 請問大家如何修改才能讓"Server端" 看到文件下載的進度 在statusbar顯示 目前傳輸大小/檔案大小 編輯記錄
ddddd688 重新編輯於 2007-04-22 18:53:08, 註解 無‧
|
dennieschang
一般會員 發表:2 回覆:24 積分:15 註冊:2002-03-13 發送簡訊給我 |
在 看到了網友 ddddd688 於網際網路程式設計討論區貼出的問題, 而且兩週以上都沒有人
回答, 相信可能是兩種情形: 1. 這問題很多人問過, 可是網友 ddddd688 沒有爬文找一下 Solution, 所以沒人回他. 2. 這問題大家都不知道該怎麼解決. 不論是哪一種, 我覺得這是很實用的東西, 所以寫了小範例跟大家分享. http://delphi.ktop.com.tw/board.php?cid=31&fid=79&tid=88435 在 Indy 的各個元件當中, 不管是 server 還是 Client, 都會有個固定的 property, 叫做 IOHandler, 如果要從正在傳遞的 Channel 當中擷取資料, 就必須透過各個 元件的 IOHandler.Intercept.OnReceive 或是 IOHandler.Intercept.OnSend. 差別在哪呢? 在於 Server 用 OnSend 可以知道送出了多少資料, Client 則是要用 OnReceive 來知道接到了多少資料, 以及是哪些資料. 但是如果您只用 OnReceive, OnSend, 想要把這資訊秀在 Form, 或者是任一元件上面, 都會發現, 進度沒辦法即時顯示, 這又是為什麼呢? 這是因為, 介面更新和 Data 傳輸使用的都是同一個 Thread, 接收的時候沒辦法同時更新. 所以, 在筆者的範例當中, 是使用了一個單獨的 Thread 來控制下載的工作, 這個Thread 在 OnReceive 的時候, 會送出介面更新要求給 MainThread 所控制的 MainForm, 所以就可以 在資料進來的同時, 也看到畫面上的數字在跑了. 在這個範例裡面, 我是更新 Listview 裡面的 Item, 但 ddddd688 想更新 StatusBar 的文字, 在概念上跟作法上是完全相同的, 還比這範例更簡單一點, 請您到上面我貼的網址連結, 從另 一個版下載完整範例. (雖然我是用 Indy 10 寫的, 但 Indy 9, 10 的差異不大, 也建議您下載 Indy 10 進行更新, 因為 Indy 10 解掉了 Indy 9 裡面很多的問題) Sorry, 這應該在筆者的書上面發表的, 但這本書出版之日目前看來還是遙遙無期... 因為出版社 跟我說, 現在 Delphi 的書沒銷量... 他們不想出... Best Regard. Dennies Chang. Delphi/Kylix Indy 網路程式設計一書作者.
------
Indy - Delphi, BCB, Kylix最好用的網路元件組. 小弟也是Indy Core Team的成員之一, 平常會在 http://www.sanbrandstudio.com/ http://delphi.ktop.com.tw/ 以及Delphi深度論壇出沒. |
hagar
版主 發表:143 回覆:4056 積分:4445 註冊:2002-04-14 發送簡訊給我 |
|
pcplayer99
尊榮會員 發表:146 回覆:790 積分:632 註冊:2003-01-21 發送簡訊給我 |
|
dennieschang
一般會員 發表:2 回覆:24 積分:15 註冊:2002-03-13 發送簡訊給我 |
|
pcplayer99
尊榮會員 發表:146 回覆:790 積分:632 註冊:2003-01-21 發送簡訊給我 |
老兄,现在才注意到你的签名档,居然是 Indy core team 的人,佩服啊。
我试用了一下 Indy 10,感觉似乎有好多 bug,现在我还是在用 Indy 9.0.18 感觉很稳定。 另外,请问 Indy 10 支持 SCTP 了吗? ===================引 用 dennieschang 文 章=================== 理解.... 說的好, 到時請李維代為引薦大陸地區的出版社, 這倒是個不錯的主意.... |
like
一般會員 發表:30 回覆:35 積分:18 註冊:2005-03-10 發送簡訊給我 |
dennieschang ,您好!您目前有没有出indy的书籍呢?
===================引 用 dennieschang 文 章=================== 理解.... 說的好, 到時請李維代為引薦大陸地區的出版社, 這倒是個不錯的主意.... |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |