Delphi File Loader Component

I need a file loader component for Delphi. Can you help me?

+4
source share
2 answers

Use the URLDownloadToFile -level URLDownloadToFile :

 uses UrlMon; ... URLDownloadToFile(nil, 'http://www.rejbrand.se/', 'C:\Users\Andreas Rejbrand\Desktop\index.html', 0, nil); 

Or you could easily write your own bootloader function using WinInet functions, something like

 uses WinInet; ... hInet := InternetOpen(PChar(UserAgent), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0); try hURL := InternetOpenUrl(hInet, PChar('http://' + Server + Resource), nil, 0, 0, 0); try repeat InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen); ... 

There are many code examples in SO. Use the search box above.

Update

I wrote a small sample. You might want to execute this code on your own stream and let it scan every 10 kB or so, so that you can provide the user with some kind of progress bar, for example.

 function DownloadFile(const UserAgent, URL, FileName: string): boolean; const BUF_SIZE = 4096; var hInet, hURL: HINTERNET; f: file; buf: PByte; amtc: cardinal; amti: integer; begin result := false; hInet := InternetOpen(PChar(UserAgent), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0); try hURL := InternetOpenUrl(hInet, PChar(URL), nil, 0, 0, 0); try GetMem(buf, BUF_SIZE); try FileMode := fmOpenWrite; AssignFile(f, FileName); try Rewrite(f, 1); repeat InternetReadFile(hURL, buf, BUF_SIZE, amtc); BlockWrite(f, buf^, amtc, amti); until amtc = 0; result := true; finally CloseFile(f); end; finally FreeMem(buf); end; finally InternetCloseHandle(hURL); end; finally InternetCloseHandle(hInet); end; end; 
+12
source

You can also do this with Indy:

 procedure DownloadHTTP(const AUrl : string; DestStream: TStream); begin with TIdHTTP.Create(Application) do try try Get(AUrl,DestStream); except On e : Exception do MessageDlg(Format('Erreur : %s',[e.Message]), mtInformation, [mbOK], 0); end; finally Free; end; end; 

If you want to download quickly, you can also use Smart Internet Suite.

+7
source

Source: https://habr.com/ru/post/1333227/


All Articles