Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Delphi - Trasferimento File con Componenti TClientSocket e TServerSocket + visualizzazione del progress Download/Upload
Forum - Delphi - Trasferimento File con Componenti TClientSocket e TServerSocket + visualizzazione del progress Download/Upload

Avatar
givex8 (Normal User)
Rookie


Messaggi: 28
Iscritto: 05/03/2011

Segnala al moderatore
Postato alle 18:14
Giovedė, 17/03/2011
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

PM Quote
Avatar
givex8 (Normal User)
Rookie


Messaggi: 28
Iscritto: 05/03/2011

Segnala al moderatore
Postato alle 18:50
Mercoledė, 04/05/2011
Salve a tutti, ho risolto in questo modo:


Programma che invia il File:
Codice sorgente - presumibilmente Delphi

  1. var
  2. F: File of byte;
  3. NumRead: Integer;
  4. Buffer: array [1..8196] of Char;
  5. begin
  6. AssignFile(F, 'C:\File.exe');
  7. Reset(F);
  8. ClientSocket1.Socket.SendText('SIZE!' + IntToStr(FileSize(F)));
  9. ProgressBar1.Max := FileSize(F);
  10. ProgressBar1.Position := 0;
  11. Sleep(200);
  12. repeat
  13.   BlockRead(F,Buffer, SizeOf(Buffer), NumRead);
  14.   Clientsocket1.Socket.SendBuf(Buffer, SizeOf(Buffer));
  15.   ProgressBar1.Position := ProgressBar1.Position + SizeOf(Buffer);
  16.   Sleep(30);
  17. until (NumRead = 0);
  18. CloseFile(F);





Programma che riceve il File:
Codice sorgente - presumibilmente Delphi

  1. var
  2. ReceivingFile : Boolean;
  3. Len : Integer;
  4. FStream: TFileStream;
  5.  
  6.  
  7. procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
  8.   Socket: TCustomWinSocket);
  9. var
  10.   iLen: Integer;
  11.   Bfr: Pointer;
  12.   Text : string;
  13. begin
  14.   if not ReceivingFile then
  15.   begin
  16.     Text := Socket.ReceiveText;
  17.     if Copy(Text, 1, 5) = 'SIZE!' then
  18.     begin
  19.       Delete(Text, 1, 5);
  20.       Len := StrToInt(Text);
  21.       ProgressBar1.Max := StrToInt(Text);
  22.       FStream := TFileStream.Create('C:\ReceivedFile.exe', fmCreate or fmShareDenyWrite);
  23.       ReceivingFile := True;
  24.     end;
  25.   end
  26.   else
  27.   begin
  28.     iLen := Socket.ReceiveLength;
  29.     GetMem(Bfr, iLen);
  30.     try
  31.       Socket.ReceiveBuf(Bfr^, iLen);
  32.       FStream.Write(Bfr^, iLen);
  33.       Progressbar1.Position := ProgressBar1.Position + iLen;
  34.     finally
  35.        FreeMem(Bfr);
  36.        if Assigned(FStream) and (FStream.Size = Len) then
  37.        begin
  38.          FStream.Free;
  39.          FStream := nil;
  40.          ReceivingFile := False;
  41.        end;
  42.     end;
  43.   end;
  44. end;



Ultima modifica effettuata da givex8 il 04/05/2011 alle 18:56
PM Quote