Aplicativo de Download

Delphi

01/02/2004

Boa Tarde pessoal!

Alguém sabe como fazer download de arquivos, programas etc... de um Servidor WEB?
Eu tentei com esta função do próprio Fórum mas não funcionou. Deu erro.

function DownloadArquivo(const Origem, Destino: String): Boolean;
const BufferSize = 1024;
var
hSession, hURL: HInternet;
Buffer: array[1..BufferSize] of Byte;
BufferLen: DWORD;
f: File;
sAppName: string;
begin
Result := False;
sAppName := ExtractFileName(Application.ExeName);
hSession := InternetOpen(PChar(sAppName),
INTERNET_OPEN_TYPE_PRECONFIG,
nil, nil, 0);
try
hURL := InternetOpenURL(hSession,
PChar(Origem),
nil,0,0,0);
try
AssignFile(f, Destino);
Rewrite(f,1);
repeat
InternetReadFile(hURL, @Buffer,
SizeOf(Buffer), BufferLen);
BlockWrite(f, Buffer, BufferLen)
until BufferLen = 0;
CloseFile(f);
ShowMessage(´Fechei Arquivo´);
Result:=True;
finally
InternetCloseHandle(hURL)
end
finally
InternetCloseHandle(hSession)
end
end;

procedure TForm1.Button1Click(Sender: TObject);
var Origem, Destino: string;
begin
Origem := ´http://www.clubedelphi.com.br/aplicativos/limUsu.zip´;
Destino := ´C:\´;

if DownloadArquivo(Origem, Destino) then
ShowMessage(´Download OK´)
else
ShowMessage(´Erro no download´)
end;

Alguém pode me ajudar??
Obrigado!


Michaell

Michaell

Curtidas 0

Respostas

Tchoninho

Tchoninho

01/02/2004

Veja a página abaixo.

[url]http://delphiforum.icft.com.br/forum/viewtopic.php?t=30940&highlight=[/url]


GOSTEI 0
Cebikyn

Cebikyn

01/02/2004

Ou se preferir uma solução prática:

uses
  URLMon;

function DownloadFile(WEB, LOCAL: string): Boolean;
begin
  try
    Result := UrlDownloadToFile(nil, PChar(WEB), PChar(LOCAL), 0, nil) = 0;
  except
    Result := False;
  end;
end;


Daí vc pode fazer:

if DownloadFile(´http://www.servidor.com/arquivo.txt´,´C:\Teste\Arquivo.txt) then
  ShowMessage(´Download OK´)
else
  ShowMessage(´Download Falhou´);



GOSTEI 0
POSTAR