idtcp 파일 다운로드 및 업로드 실현
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
StdCtrls;
type
TForm1 = class(TForm)
btnReceive: TButton;
IdTCPClient1: TIdTCPClient;
btnSend: TButton;
procedure btnReceiveClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btnSendClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btnReceiveClick(Sender: TObject);
var
rbyte:array[0..4096] of byte;
sFile:TFileStream;
iFileSize:integer;
begin
iFileSize:=IdTCPClient1.ReadInteger;
sFile:=TFileStream.Create('xx.pdf',fmCreate);
While iFileSize>4096 do
begin
IdTCPClient1.ReadBuffer(rbyte,4096);// .ReadBuffer(rbyte,iLen);
sFile.Write(rByte,4096);
inc(iFileSize,-4096);
end;
IdTCPClient1.ReadBuffer(rbyte,iFileSize);// .ReadBuffer(rbyte,iLen);
sFile.Write(rByte,iFileSize);
sFile.Free;
ShowMessage('file get ok!');
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
// IdTCPClient1.BoundIP:= '127.0.0.1';
// IdTCPClient1.BoundPort:= 90011;
IdTCPClient1.Host:= '127.0.0.1';
IdTCPClient1.Port:= 9000;
IdTcpClient1.Connect(5000);
self.Caption:= ' ';
end;
procedure TForm1.btnSendClick(Sender: TObject);
var
iFileHandle:integer;
iFileLen,cnt:integer;
buf:array[0..4096] of byte;
begin
// IdTCPClient1.Host:='127.0.0.1';
// IdTCPClient1.Port:=9000;
//IdTCPClient1.Connect(5000);
if IdTCPClient1.Connected then
begin
iFileHandle:=FileOpen('xx.pdf',fmOpenRead);
iFileLen:=FileSeek(iFileHandle,0,2); //2 :soFromBeginning : from the end of the file
FileSeek(iFileHandle,0,0);
// ProgressBar1.Max:=iFileLen;
// ProgressBar1.Position := 0;
IdTCPClient1.WriteLn('xx.pdf'+'|'+IntToStr(iFileLen));
while true do
begin
Application.ProcessMessages;
cnt:=FileRead(iFileHandle,buf,4096);
IdTCPClient1.WriteBuffer(buf,cnt);
// IdTCPClient1.WriteInteger(cnt);
// ProgressBar1.Position:=ProgressBar1.Position + cnt;
// StatusBar1.Panels[0].Text:=' ...';
if cnt<4096 then
break;
end;
FileClose(iFileHandle);
// Label2.Caption:=' !';
// StatusBar1.Panels[0].Text:=' !';
end;
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPServer;
type
TForm1 = class(TForm)
IdTCPServer1: TIdTCPServer;
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
procedure FormCreate(Sender: TObject);
procedure IdTCPServer1Connect(AThread: TIdPeerThread);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses IdSocketHandle;
//server
procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
rbyte:array[0..4096] of byte;
sFile:TFileStream;
cnt,cmd,FileSize:integer;
str,FileName:string;
begin
str:=AThread.Connection.ReadLn; //
cmd:=pos('|',str); //
FileName:=copy(str,1,cmd-1); //
//IdTCPClient1.WriteLn(ExtractFileName(edtFileName.Text)+'|'+IntToStr(iFileLen));
FileSize:=StrToInt(copy(str,cmd+1,Length(str)-cmd+1)); //
if MessageBox(0,Pchar(' '+AThread.Connection.Socket.Binding.PeerIP+' "'+FileName+'" ?'),' ',MB_YesNo or MB_ICONQUESTION)=ID_Yes then //
begin
// ProgressBar1.Max:=FileSize; //
// ProgressBar1.Position:=0;
// SaveDialog1.FileName:=FileName; // , SaveDialog1.Execute; ,
// SaveDialog1.Execute;
//sFile:=TFileStream.Create(SaveDialog1.FileName,fmCreate); //
sFile:=TFileStream.Create(FileName,fmCreate); //
While FileSize>4096 do
begin
AThread.Connection.ReadBuffer(rbyte,4096);//
sFile.Write(rByte,4096); //
// cnt:=AThread.Connection.ReadInteger; //
// ProgressBar1.Position:=ProgressBar1.Position+cnt; //
// Label1.Caption:=' ..';
// StatusBar1.Panels[0].Text:=' ...';
inc(FileSize,-4096);
end;
AThread.Connection.ReadBuffer(rbyte,FileSize);// .ReadBuffer(rbyte,iLen);
sFile.Write(rByte,FileSize);
sFile.Free;
// StatusBar1.Panels[0].Text:=' !';
// Label1.Caption:=' !';
end;
END;
procedure TForm1.FormCreate(Sender: TObject);
var
h:TIdSocketHandle;
begin
begin
IdTCPServer1.DefaultPort:=9000;
// h:= IdTCPServer1.Bindings.Add;
// h.IP:= '127.0.0.1';
// h.Port:=90011;
IdTCPServer1.Active:=True;
Self.Caption:= ' ';
end;
end;
//
procedure TForm1.IdTCPServer1Connect(AThread: TIdPeerThread);
var
iFileHandle:integer;
iFileLen,cnt:integer;
buf:array[0..4096] of byte;
begin
iFileHandle:=FileOpen('xx.pdf',fmOpenRead);
iFileLen:=FileSeek(iFileHandle,0,2);
FileSeek(iFileHandle,0,0);
AThread.Connection.WriteInteger(iFileLen);
while true do
begin
cnt:=FileRead(iFileHandle,buf,4096);
AThread.Connection.WriteBuffer(buf,cnt);
if cnt<4096 then
break;
end;
FileClose(iFileHandle);
end;
end.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.