How can I download a file from its download link provided on a webpage using Delphi? Please note that I am not using visual components. I am using IHTMLDocument2 to access the web.
Asked
Active
Viewed 246 times
0
-
2[WinInet](https://stackoverflow.com/questions/4586288/can-i-load-a-memo-or-rich-edit-from-a-text-file-on-server/4586333#4586333), [Indy](https://stackoverflow.com/a/2185308/282848), [UrlMon](https://stackoverflow.com/a/3506314/282848)... Really, there are many SO questions about this. – Andreas Rejbrand Dec 03 '19 at 08:40
2 Answers
3
For all modern Delphi version since Delphi XE8 you can use THTTPClient
from unit System.Net.HttpClient
:
Procedure SimpleDownload(const DownloadUrl: string; Stream: TStream);
var
Client: THTTPClient;
Response: IHTTPResponse;
begin
Client := THTTPClient.Create;
try
Response := Client.Get(DownloadUrl, Stream);
if Response.StatusCode = 200 then
// Success case
else
// Error case: Check Response.StatusText or StatusCode
finally
Client.Free;
end;
end;
For download to a file call SimpleDownload
with an instance of TFileStream
.

Schneider Infosystems Ltd
- 1,487
- 16
- 24
1
I'm assuming that IHTMLDocument2
is the API from the MSHTML library, because AFAIK neither VCL nor CLX offers an API named as such.
As far as I know, MSHTML does not implement a generic download API - sometimes called a "HTTP client". You might be interested in using fcl-web from the Lazarus project - its a library meant for developing server applications but it also includes an HTTP client API named fphttpclient
.

Guss
- 30,470
- 17
- 104
- 128
-
Thank you. This is the working code I am using in my app now: errcode := URLMon.URLDownloadToFile(nil, PChar('https://fonts.ge/ka/download/font/704/ALK-Sanet/alk-sanet_[FONTS.GE].zip'),PChar( 'D:\test.zip'),0,nil); – Rati2019 Dec 03 '19 at 09:19
-
1That looks like it could work, but @schneider-infosystems-ltd's solution seems like a much better idea. – Guss Dec 03 '19 at 09:49