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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
| uses Wininet;
function FtpDownloadFile(strHost, strUser, strPwd: string;
Port: Integer; ftpDir, ftpFile, TargetFile: string;
ProxyUser,ProxyPassword:string;
ALabel:TLabel=nil;AProgressBar: TProgressBar=nil): Boolean;
function FmtFileSize(Size: cardinal): string;
begin
if Size >= $3B9ACA00 then
Result := Format('%.2f', [Size / $3B9ACA00]) + ' Go'
else
if Size >= $F4240 then
Result := Format('%.2f', [Size / $F4240]) + ' Mo'
else
if Size < 1000 then
Result := IntToStr(Size) + ' bytes'
else
Result := Format('%.2f', [Size / 1000]) + ' Ko';
end;
const
READ_BUFFERSIZE = 4096; // ou 256, 512, ...
var
hNet, hFTP, hFile: HINTERNET;
buffer: array[0..READ_BUFFERSIZE - 1] of Char;
bufsize, dwBytesRead, fileSize: DWORD;
sRec: TWin32FindData;
strStatus: string;
LocalFile: file;
bSuccess: Boolean;
PC:integer; //PourCentage de téléchargement
UnFichier:TFileStream;
begin
Result := False;
{ Ouvre une session internet }
hNet := InternetOpen('Nom du programme', // Agent
INTERNET_OPEN_TYPE_PRECONFIG, // AccessType
PChar(ProxyUser), // Nom utilisateur pour le Proxy
PChar(ProxyPassword), // Mot de passe Proxy
0); // ou INTERNET_FLAG_ASYNC / INTERNET_FLAG_OFFLINE
{
l'Agent contient le nom de l'application ou de
l'entité appelant les fonctions Internet
}
{ See if connection handle is valid }
if hNet = nil then
begin
ShowMessage('Impossible d''accéder à WinInet.Dll');
Exit;
end;
{ Connect to the FTP Server }
hFTP := InternetConnect(hNet, // Handle from InternetOpen
PChar(strHost), // FTP server
port,//ou bien (INTERNET_DEFAULT_FTP_PORT),
PChar(StrUser), // username
PChar(strPwd), // password
INTERNET_SERVICE_FTP, // FTP, HTTP, or Gopher?
INTERNET_FLAG_PASSIVE, // flag: 0 or INTERNET_FLAG_PASSIVE
0);// Nombre défini par l'utilisateur pour un callback
if hFTP = nil then
begin
InternetCloseHandle(hNet);
ShowMessage(Format('L''Hôte "%s" n''est pas disponible',[strHost]));
Exit;
end;
{ Changer de répertoire }
bSuccess := FtpSetCurrentDirectory(hFTP, PChar(ftpDir));
if not bSuccess then
begin
InternetCloseHandle(hFTP);
InternetCloseHandle(hNet);
ShowMessage(Format('Ne peut accéder au répertoire %s.',[ftpDir]));
Exit;
end;
{ Lecture de la taille du fichier }
if FtpFindFirstFile(hFTP, PChar(ftpFile), sRec, 0, 0) <> nil then
begin
fileSize := sRec.nFileSizeLow;
end else
begin
InternetCloseHandle(hFTP);
InternetCloseHandle(hNet);
ShowMessage(Format('Cannot find file ',[ftpFile]));
Exit;
end;
{ Ouvre le fichier }
hFile := FtpOpenFile(hFTP, // Handle d'une session ftp
PChar(ftpFile), // Nom du fichier
GENERIC_READ, // dwAccess
FTP_TRANSFER_TYPE_BINARY, // dwFlags
0); // Ceci est le contexte utiliser pour les Callback.
if hFile = nil then
begin
InternetCloseHandle(hFTP);
InternetCloseHandle(hNet);
Exit;
end;
{ Créer un nouveau fichier local }
if FileExists(TargetFile) then DeleteFile(TargetFile);
UnFichier:=TFileStream.Create(TargetFile,fmCreate);
try
dwBytesRead := 0;
bufsize := READ_BUFFERSIZE;
while (bufsize > 0) do
begin
Application.ProcessMessages;
if not InternetReadFile(hFile,
@buffer, // addresse d'un buffer qui reçoit les données
READ_BUFFERSIZE, // Nombre d'octets à lire et à placer dans le buffer
bufsize) // Nombre d'octets effectivement lus
then Break; //On a fini de recevoir quelque chose
if (bufsize > 0) and (bufsize <= READ_BUFFERSIZE)
then bufsize:=UnFichier.Write(buffer, bufsize);
dwBytesRead := dwBytesRead + bufsize;
{ Montrer Progression }
PC:=Round(dwBytesRead * 100 / fileSize);
if Assigned(AProgressBar)
then AProgressBar.Position :=PC;
if Assigned(ALabel)
then ALabel.Caption := Format('%s of %s / %d %%',[FmtFileSize(dwBytesRead),FmtFileSize(fileSize) ,PC]);
end;
Result := True;
finally
UnFichier.Free;
InternetCloseHandle(hFile);
InternetCloseHandle(hFTP);
InternetCloseHandle(hNet);
end;
end; |
Partager