17/03/11 17:14
givex8
Salve a tutti, ho bisogno di realizzare un'applicazione che mi permette di trasferire file e sapere da entrambi i lati (client e server) il progresso del trasferimento...
Ho provato con SendStream e ReceiveBuf ma così facendo non riesco a "sapere" il progresso...
Potete farmi un esempio di trasferimento file + visualizzazione del progresso di trasferimento?
Grazie mille a tutti
PS: Scusate per prima... Per sbaglio ho aperto una Domanda invece della Discussione... Potete chiudere la Domanda... Scusate Ancora
aaa
04/05/11 16:50
givex8
Salve a tutti, ho risolto in questo modo:
Programma che invia il File:
var
F: File of byte;
NumRead: Integer;
Buffer: array [1..8196] of Char;
begin
AssignFile(F, 'C:\File.exe');
Reset(F);
ClientSocket1.Socket.SendText('SIZE!' + IntToStr(FileSize(F)));
ProgressBar1.Max := FileSize(F);
ProgressBar1.Position := 0;
Sleep(200);
repeat
BlockRead(F,Buffer, SizeOf(Buffer), NumRead);
Clientsocket1.Socket.SendBuf(Buffer, SizeOf(Buffer));
ProgressBar1.Position := ProgressBar1.Position + SizeOf(Buffer);
Sleep(30);
until (NumRead = 0);
CloseFile(F);
Programma che riceve il File:
var
ReceivingFile : Boolean;
Len : Integer;
FStream: TFileStream;
procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
iLen: Integer;
Bfr: Pointer;
Text : string;
begin
if not ReceivingFile then
begin
Text := Socket.ReceiveText;
if Copy(Text, 1, 5) = 'SIZE!' then
begin
Delete(Text, 1, 5);
Len := StrToInt(Text);
ProgressBar1.Max := StrToInt(Text);
FStream := TFileStream.Create('C:\ReceivedFile.exe', fmCreate or fmShareDenyWrite);
ReceivingFile := True;
end;
end
else
begin
iLen := Socket.ReceiveLength;
GetMem(Bfr, iLen);
try
Socket.ReceiveBuf(Bfr^, iLen);
FStream.Write(Bfr^, iLen);
Progressbar1.Position := ProgressBar1.Position + iLen;
finally
FreeMem(Bfr);
if Assigned(FStream) and (FStream.Size = Len) then
begin
FStream.Free;
FStream := nil;
ReceivingFile := False;
end;
end;
end;
end;
Ultima modifica effettuata da givex8 04/05/11 16:56
aaa