Rati2019
Rati2019

Reputation: 91

How can I download a file from its download link provided on a webpage using Delphi?

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.

Upvotes: 0

Views: 395

Answers (2)

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.

Upvotes: 3

Guss
Guss

Reputation: 32315

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.

Upvotes: 1

Related Questions