T
tl_lyq
Unregistered / Unconfirmed
GUEST, unregistred user!
以下是服务端接收一个文件时。如果同时接收多个文件,那么进度条显示就不正确了。
如何改成接收每个文件时有自己的窗体,在自己的窗体显示进度,这样,可以随时取消传输 。
procedure TfmServer.IdTCPServer1Execute(AThread: TIdPeerThread);
var
aFileSize : Integer;
aFileName : String;
Buff : array[0..1023] of Byte;
ReadCount : Integer;
begin
with AThread.Data as TThreadData do
begin
if State = dstNone then
begin
if not AThread.Terminated and AThread.Connection.Connected then
begin
//读取文件名
aFileName := AThread.Connection.ReadLn(#13#10, 100);
if aFileName = '' then
Exit;
SaveDialog1.FileName := aFileName;
if SaveDialog1.Execute then
begin
//返回确认文件传输标志
AThread.Connection.WriteLn;
//开始读取文件长度,创建文件
AThread.Connection.ReadBuffer(aFileSize, 4);
FileSize := aFileSize;
ProgressBar1.Max := FileSize;
Stream := TFileStream.Create(SaveDialog1.FileName, fmCreate);
State := dstReceiving;
end
else
AThread.Connection.Disconnect
end;
end else begin
if not AThread.Terminated and AThread.Connection.Connected then
begin
//读取文件流
repeat
if FileSize - Stream.Size > SizeOf(Buff) then
ReadCount := SizeOf(Buff)
else
ReadCount := FileSize - Stream.Size;
AThread.Connection.ReadBuffer(Buff, ReadCount);
Stream.WriteBuffer(Buff, ReadCount);
ProgressBar1.Position := Stream.Size;
Caption := IntToStr(Stream.Size) + '/' + IntToStr(FileSize);
Application.ProcessMessages;
until Stream.Size >= FileSize;
AThread.Connection.WriteLn('OK');
Stream.Free;
Stream := nil;
State := dstNone;
end;
end;
end;
end;
如何改成接收每个文件时有自己的窗体,在自己的窗体显示进度,这样,可以随时取消传输 。
procedure TfmServer.IdTCPServer1Execute(AThread: TIdPeerThread);
var
aFileSize : Integer;
aFileName : String;
Buff : array[0..1023] of Byte;
ReadCount : Integer;
begin
with AThread.Data as TThreadData do
begin
if State = dstNone then
begin
if not AThread.Terminated and AThread.Connection.Connected then
begin
//读取文件名
aFileName := AThread.Connection.ReadLn(#13#10, 100);
if aFileName = '' then
Exit;
SaveDialog1.FileName := aFileName;
if SaveDialog1.Execute then
begin
//返回确认文件传输标志
AThread.Connection.WriteLn;
//开始读取文件长度,创建文件
AThread.Connection.ReadBuffer(aFileSize, 4);
FileSize := aFileSize;
ProgressBar1.Max := FileSize;
Stream := TFileStream.Create(SaveDialog1.FileName, fmCreate);
State := dstReceiving;
end
else
AThread.Connection.Disconnect
end;
end else begin
if not AThread.Terminated and AThread.Connection.Connected then
begin
//读取文件流
repeat
if FileSize - Stream.Size > SizeOf(Buff) then
ReadCount := SizeOf(Buff)
else
ReadCount := FileSize - Stream.Size;
AThread.Connection.ReadBuffer(Buff, ReadCount);
Stream.WriteBuffer(Buff, ReadCount);
ProgressBar1.Position := Stream.Size;
Caption := IntToStr(Stream.Size) + '/' + IntToStr(FileSize);
Application.ProcessMessages;
until Stream.Size >= FileSize;
AThread.Connection.WriteLn('OK');
Stream.Free;
Stream := nil;
State := dstNone;
end;
end;
end;
end;