How to download file from the internet

 

2002-11-25 19:10:55  Gustav Evertsson   Ratings: 8, Votes: 1

 

function GetInetFile

(const fileURL, FileName: 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(fileURL),

            nil,0,0,0);

  try

   AssignFile(f, FileName);

   Rewrite(f,1);

   repeat

    InternetReadFile(hURL, @Buffer,

                     SizeOf(Buffer), BufferLen);

    BlockWrite(f, Buffer, BufferLen)

   until BufferLen = 0;

   CloseFile(f);

   Result:=True;

  finally

   InternetCloseHandle(hURL)

  end

finally

  InternetCloseHandle(hSession)

end

end;

 

 

procedure TForm1.button1Click(Sender: TObject);

var  InternetFile,LocalFile: string;

begin

InternetFile:=' http://www.delphitips.com/images/delphispirit.gif';

LocalFile:='c:/delphispirit.gif';

 

if GetInetFile(IntrnetFile,LocalFile)=True then

   ShowMessage('download')

else

  ShowMessage('Can not download the updated files');

end;