Charger un fichier sur Internet

uses WinInet;
{$R *.DFM}

function DownloadFileFromHTTP( const AURL: string; const ADestFileName: string): boolean;
var
hInet: HINTERNET;
hFile: HINTERNET;
localFile: file;
buffer: array[1..1024] of byte;
bytesRead: DWORD;
begin
result := False;
hInet := InternetOpen(PChar(application.title),
INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
hFile := InternetOpenURL(hInet, PChar(AURL), nil, 0, 0, 0);
if Assigned(hFile) then
begin
AssignFile(localFile, ADestFileName);
Rewrite(localFile, 1);
repeat
InternetReadFile(hFile, @buffer, SizeOf(buffer), bytesRead);
BlockWrite(localFile, buffer, bytesRead);
until bytesRead = 0;
CloseFile(localFile);
result := true;
InternetCloseHandle(hFile);
end;
InternetCloseHandle(hInet);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if DownloadFileFromHTTP('http://www.fobec.com', 'c:index.htm') then
ShowMessage('opération réussi')
else
ShowMessage('echec lors du download');
end;
Commentaire des internautes
avatar
Fobec le 26/02/2009
//une variante avec UrlMon
uses
UrlMon;

function GetPageContent(URL: string): string;
var
MyFile: file;
Buf: array[0..14] of Char;
Filename: string;
MyResult: integer;
begin
Result := '\';
Filename := ExtractFilePath(ParamStr(0)) + 'temp.txt';
UrlDownloadToFile(nil, PChar(URL), PChar(Filename), 0, nil);
AssignFile(MyFile, Filename);
{$i-}
if Fileexists(Filename) then
Reset(MyFile)
else
ReWrite(MyFile);
{$I+}
if IOResult = 0 then
BlockRead(MyFile, Buf, SizeOf(Buf), MyResult);
CloseFile(MyFile);
DeleteFile(Filename);
Result := Buf;
end;
Ajouter un commentaire
Pseudo
Adresse email
Site internet (optionnel)
Votre commentaire
Fiche de l'article
Mise a jour29/06/2006
VisualisationVu 1974 fois
PublicInternaute zz
CategorieDelphi - Trucs et astuces - Internet
Auteur de l'article
arachnosoft arachnosoft
connecté le 02/11/2011
1 articles dans la section
Contacter l'auteur