Difficulté avec TFTPSend et les barres de progression
Bonjour,
Malgré des recherches sur les forums, je n'ai pas trouvé de réponse à mon souci.
J'ai chargé Synapse qui me permet de télécharger un fichier distant avec ftpsend.
J'ai essayé d'y ajouter une barre de progression mais les exemples que j'ai pu trouver ça et là me donnent trop souvent des erreurs d'incompatibilé ou ce composant absent avec
Code:
FTPClient := TFTPSend.Create et FileStream := TFileStream.Create(LocalFileName, fmCreate);
Est-ce que quelqu'un aurait un lien vers quelque chose qui pourrait m'aider ?
Fonction qui marche :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| function DownloadFileFromFTP(RemoteName, LocalFileName: string): Boolean;
var
//'ftp://dyia_bta_alpin@ftp.agilsport.fr/EET_Calculator_install_1.1.0.exe';
strFTPHost: String;
strFTPuser: String;
strFTPpw: String;
LocalFile: String;
FTPClient : TFTPSend;
RemoteDir: String;
RemoteFile: String;
rc: boolean;
size: Integer ;
begin
strFTPHost:= 'ftp.xxxt.fr';
strFTPuser:= 'yyy';
strFTPpw:= '*******';
LocalFile:= LocalFileName;
//RemoteFile:= RemoteName;
RemoteFile:= 'EET_Calculator_install_1.1.0.exe';
FTPClient := TFTPSend.Create;
with FTPClient do begin
try
TargetPort := '21';
TargetHost := strFtpHost;
UserName := strFTPuser;
Password := strFTPpw;
//----------------------------------
// bail out if the FTP connect fails
if not LogIn then
begin
ShowMessage('Erreur / FTP login');
exit;
end;
size := FileSize(RemoteFile);
ShowMessage(IntToStr(size));
// Set filename to FTP
DirectFileName := LocalFileName;
DirectFile := True;
// change directory if requested
if RemoteDir <> '' then ChangeWorkingDir(RemoteDir);
// STOR file to FTP server.
// rc := StoreFile(RemoteFile,false);
// RETR file from FTP server.
rc := RetrieveFile(RemoteFile, False);
// close the connection
LogOut;
Except On E: Exception do begin
ShowMessage('Error: ' + E.Message );
end;
end;
// free the FTP client object
free;
end;
end; |
Merci
Philippe
FTPSend (Synapse) et barre de progression - Résolu
Bonjour,
Merci pour les informations et les conseils donnés.
Mon environnement : Windows 11 64 bits, l'application étant compilée pour une plateforme 32 bits.
Synapse : # (Ararat) Synapse TCP/IP library for Pascal - Official source repository is https://github.com/geby/synapse (It was changed from SourceForge at January 2024).
Lazarus v : 3.6
Dans les exemples proposés qui sont souvent trop anciens, il y a souvent des propriétés absentes, qui ont changé de nom ou qui ne correspondent plus au repository de Synapse téléchargé.
En plus les propriétés des contrôles peuvent être différentes entre Delphi et Lazarus.
Après avoir testé beaucoup de choses, voici ce qui a marché chez moi :
RemoteFile doit commencer par /.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
function DownloadFileFromFTP(RemoteFile, LocalFileName: string): Boolean;
var
strFTPHost, strFTPuser, strFTPpw: String;
FTPClient: TFTPSend;
FileStream: TFileStream;
Buffer: array[0..8191] of Byte; // Un buffer de 8 Ko pour lire en segments
BytesRead, TotalBytes, FileSize: Int64;
begin
Result := False;
Form2.LbDownload.Visible := True;
Application.ProcessMessages;
// Paramètres FTP
strFTPHost := 'ftp.xxxxxx.fr';
strFTPuser := '-----';
strFTPpw := '------';
FTPClient := TFTPSend.Create;
try
FTPClient.UserName := strFTPuser;
FTPClient.Password := strFTPpw;
FTPClient.TargetHost := strFTPHost;
FTPClient.PassiveMode := True;
if FTPClient.Login then
begin
// Obtenir la taille du fichier distant
FileSize := FTPClient.FileSize(RemoteFile);
if FileSize <= 0 then
Exit;
// Configurer la barre de progression
Form2.ProgressBar1.Max := FileSize;
// Créer un flux pour le fichier local
FileStream := TFileStream.Create(LocalFileName, fmCreate);
try
// Télécharger le fichier du serveur FTP
if FTPClient.RetrieveFile(RemoteFile, False) then
begin
TotalBytes := 0;
FTPClient.DataStream.Position := 0;
// Lire et copier en segments pour mettre à jour la barre de progression
repeat
BytesRead := FTPClient.DataStream.Read(Buffer, SizeOf(Buffer));
if BytesRead > 0 then
begin
FileStream.WriteBuffer(Buffer, BytesRead);
TotalBytes := TotalBytes + BytesRead;
Form2.ProgressBar1.Position := TotalBytes;
// Forcer la mise à jour de l'interface utilisateur
Application.ProcessMessages;
end;
until BytesRead = 0;
//FileStream.CopyFrom(FTPClient.DataStream, FTPClient.DataStream.Size);
Result := True;
end;
finally
FileStream.Free;
end;
FTPClient.Logout;
end;
finally
FTPClient.Free;
end;
Form2.BtClose.Visible := True;
end; |
Philippe :D