Pascal Scripting: DownloadTemporaryFile

Prototype:

function DownloadTemporaryFile(const Url, FileName, RequiredSHA256OfFile: String; const OnDownloadProgress: TOnDownloadProgress): Int64;

Description:

Downloads the file from the specified URL to a temporary directory using the specified name. To find the location of the temporary directory, use ExpandConstant('{tmp}').

If RequiredSHA256OfFile is set it will compare this to the SHA-256 of the downloaded file and raise an exception if the hashes don't match.

An exception will be raised if there was an error. Otherwise, returns the number of bytes downloaded. Returns 0 if RequiredSHA256OfFile is set and the file was already downloaded.

Supports HTTPS (but not expired or self-signed certificates) and HTTP. Redirects are automatically followed and proxy settings are automatically used. Safe to use from services.

For basic authentication use SetDownloadCredentials.

Set OnDownloadProgress to a function to be informed of progress, or nil otherwise.

Remarks:

TOnDownloadProgress is defined as:

TOnDownloadProgress = function(const Url, FileName: string; const Progress, ProgressMax: Int64): Boolean;

ProgressMax will be 0 if the file size is still unknown. Return True to allow the download to continue, False otherwise.

Example:
[Code]
function OnDownloadProgress(const Url, Filename: string; const Progress, ProgressMax: Int64): Boolean;
begin
  if ProgressMax <> 0 then
    Log(Format('  %d of %d bytes done.', [Progress, ProgressMax]))
  else
    Log(Format('  %d bytes done.', [Progress]));
  Result := True;
end;

function InitializeSetup: Boolean;
begin
  try
    DownloadTemporaryFile('https://jrsoftware.org/download.php/is.exe', 'innosetup-latest.exe', '', @OnDownloadProgress);
    DownloadTemporaryFile('https://jrsoftware.org/download.php/iscrypt.dll', 'ISCrypt.dll', '2f6294f9aa09f59a574b5dcd33be54e16b39377984f3d5658cda44950fa0f8fc', @OnDownloadProgress);
    Result := True;
  except
    Log(GetExceptionMessage);
    Result := False;
  end;
end;

See CodeDownloadFiles.iss for another example which uses CreateDownloadPage instead.

See also:

SetDownloadCredentials
DownloadTemporaryFileSize
DownloadTemporaryFileDate
CreateDownloadPage
ExtractTemporaryFile