檔案對傳 |
答題得分者是:japhen
|
sky810675
一般會員 發表:5 回覆:23 積分:5 註冊:2006-07-21 發送簡訊給我 |
|
japhen
中階會員 發表:2 回覆:57 積分:71 註冊:2006-08-16 發送簡訊給我 |
|
sky810675
一般會員 發表:5 回覆:23 積分:5 註冊:2006-07-21 發送簡訊給我 |
|
japhen
中階會員 發表:2 回覆:57 積分:71 註冊:2006-08-16 發送簡訊給我 |
如果不考慮防火牆穿透的問題,是可以用indy的idTCPServer跟idTCPClient完成你的問題
例子我直接寫在這,以下使用indy 10的版本,如果你的是indy 9 ,請先上indy的官方網站下載更新 第一個程式是server端的,先開一個空的程式,上面放一個idTCPServer元件,屬性defaultport設3001 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, IdBaseComponent, IdComponent, IdCustomTCPServer, IdTCPServer,IdContext; type TForm1 = class(TForm) IdTCPServer1: TIdTCPServer; procedure FormCreate(Sender: TObject); procedure IdTCPServer1Execute(AContext: TIdContext); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); begin idTCPServer1.Active:=true; end; procedure TForm1.IdTCPServer1Execute(AContext: TIdContext); var fs : int64; readstr,reqstr : string ; mms : TMemoryStream; begin while AContext.Connection.Connected do begin AContext.Connection.Socket.WriteLn('demo_file_transfer'); readstr:=AContext.Connection.Socket.ReadLn; if copy(readstr,1,9)='GET_FILE_' then begin reqstr:=copy(readstr,10,length(readstr)-9); if fileexists(reqstr) then begin mms:=TMemoryStream.Create; mms.LoadFromFile(reqstr); fs:=mms.Size; AContext.Connection.Socket.WriteLn('READY_TRANSFERFILE_' inttostr(fs)); AContext.Connection.Socket.Write(mms,fs); mms.free; end; end; end; end; end. 第二個例子是client端,也是做一個新的程式,放一個button在form上就好 unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject); var filesize : int64; readx :string ; mms : TMemoryStream; begin if fileexists('temp.txt') then deletefile('temp.txt'); with TidTCPClient.Create(self) do begin host:='127.0.0.1'; port:=3001; try connect; finally if connected and (socket.ReadLn='demo_file_transfer') then begin socket.WriteLn('GET_FILE_c:\windows\system.ini'); readx:=socket.ReadLn; if copy(readx,1,19)='READY_TRANSFERFILE_' then begin filesize := strtointdef(copy(readx,20,length(readx)-19),0); if filesize>0 then begin mms:=TMemoryStream.Create; socket.ReadStream(mms,filesize); mms.SaveToFile('temp.txt'); mms.Free; end; end; end; disconnect; end; free; end; end; end. 先執行server端,再打開client端程式,點下client端的button,你會發現client程式所在的資料夾中,多了一個temp.txt的檔案 ................有事忙先在此打住,你先練練,有問題再討論
------
歡迎在大陸的台商加入我們的行列 台商小棧 www.twgocn.com |
sky810675
一般會員 發表:5 回覆:23 積分:5 註冊:2006-07-21 發送簡訊給我 |
|
japhen
中階會員 發表:2 回覆:57 積分:71 註冊:2006-08-16 發送簡訊給我 |
|
sky810675
一般會員 發表:5 回覆:23 積分:5 註冊:2006-07-21 發送簡訊給我 |
|
sky810675
一般會員 發表:5 回覆:23 積分:5 註冊:2006-07-21 發送簡訊給我 |
|
japhen
中階會員 發表:2 回覆:57 積分:71 註冊:2006-08-16 發送簡訊給我 |
|
sky810675
一般會員 發表:5 回覆:23 積分:5 註冊:2006-07-21 發送簡訊給我 |
|
japhen
中階會員 發表:2 回覆:57 積分:71 註冊:2006-08-16 發送簡訊給我 |
OPENDIALOG肯定不行,你要OPEN什麼?當然是來自遠端的檔案,但那種DIALOG就肯定不是系統所提供的,而是你自己寫的對話框~
至於SAVEDIALOG那是你客戶端要存放檔案的地方,用SAVEDIALOG來決定把檔案放哪,這當然是最好~~ 在上述的例子中,客戶端要執行 mms.SaveToFile('temp.txt'); 之前加一行 IF SAVEDIALOG.EXECUTE THEN 當然,在CLIENT的FORM上一定要放一個SAVEDIALOG
------
歡迎在大陸的台商加入我們的行列 台商小棧 www.twgocn.com |
sky810675
一般會員 發表:5 回覆:23 積分:5 註冊:2006-07-21 發送簡訊給我 |
|
japhen
中階會員 發表:2 回覆:57 積分:71 註冊:2006-08-16 發送簡訊給我 |
|
sky810675
一般會員 發表:5 回覆:23 積分:5 註冊:2006-07-21 發送簡訊給我 |
|
japhen
中階會員 發表:2 回覆:57 積分:71 註冊:2006-08-16 發送簡訊給我 |
|
sky810675
一般會員 發表:5 回覆:23 積分:5 註冊:2006-07-21 發送簡訊給我 |
|
japhen
中階會員 發表:2 回覆:57 積分:71 註冊:2006-08-16 發送簡訊給我 |
------
歡迎在大陸的台商加入我們的行列 台商小棧 www.twgocn.com |
sky810675
一般會員 發表:5 回覆:23 積分:5 註冊:2006-07-21 發送簡訊給我 |
|
sky810675
一般會員 發表:5 回覆:23 積分:5 註冊:2006-07-21 發送簡訊給我 |
|
sky810675
一般會員 發表:5 回覆:23 積分:5 註冊:2006-07-21 發送簡訊給我 |
|
sky810675
一般會員 發表:5 回覆:23 積分:5 註冊:2006-07-21 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |