How to continue download on error in Inno Setup?

Viewed 116

My Inno Setup script downloads some resources using built-in functionalities. It creates Download Wizard Page:

DownloadPage := CreateDownloadPage(SetupMessage(msgWizardPreparing), SetupMessage(msgPreparingDesc), @OnDownloadProgress);

It adds several items that should be downloaded:

if WizardIsTaskSelected('taskA') then
  begin
    DownloadPage.Add('https://randomresource/taskA.zip', 'taskA.zip', '');
  end;
if WizardIsTaskSelected('taskB') then
  begin
    DownloadPage.Add('https://randomresource/taskB.zip', 'taskB.zip', '');
  end;

and last step is to show the Wizard Page and start downloading:

try
  try
    DownloadPage.Download;
    Result := True;
  except
    SuppressibleMsgBox(AddPeriod(GetExceptionMessage), mbCriticalError, MB_OK, IDOK);
    Result := False;
  end;
finally
  DownloadPage.Hide;
end;

All of the above is basically from examples provided by Inno Setup. There is one issue: if any of the downloads fails (throws exception or anything) it interrupts the whole download process and other items will not be downloaded. I would want it to continue downloading the rest of the files. I went through the Inno Setup documentation and didn't find any flag which would enable that. Is there a solution for that? Thanks in advance.

1 Answers

A simple solution is to download each file separately.

The below code will allow user to select what to do on each file's download error:

  • Retry the download
  • Skip the download
  • Abort the installation.
var
  DownloadPage: TDownloadWizardPage;

function RobustDownload(
  Url, BaseName, RequiredSHA256OfFile: String): Boolean;
var
  Retry: Boolean;
  Answer: Integer;
begin
  repeat
    try
      DownloadPage.Clear;
      DownloadPage.Add(Url, BaseName, RequiredSHA256OfFile);
      DownloadPage.Download;
      Retry := False;
      Result := True;
    except
      if DownloadPage.AbortedByUser then
      begin
        Log('Aborted by user.')
        Result := False;
        Retry := False;
      end
        else
      begin
        // Make sure the page displays the URL that fails to download
        DownloadPage.Msg2Label.Caption := Url;
        Answer :=
          SuppressibleMsgBox(
            AddPeriod(GetExceptionMessage),
            mbCriticalError, MB_ABORTRETRYIGNORE, IDABORT);
        Retry := (Answer = IDRETRY);
        Result := (Answer <> IDABORT);
      end;
    end;
  until not Retry;
end;

function NextButtonClick(CurPageID: Integer): Boolean;
begin
  if CurPageID = wpReady then
  begin
    try
      DownloadPage :=
        CreateDownloadPage(
          SetupMessage(msgWizardPreparing), SetupMessage(msgPreparingDesc),
          @OnDownloadProgress);

      DownloadPage.Show;

      Result :=
        RobustDownload('https://example.com/setup1.exe', 'setup1.exe', '') and
        RobustDownload('https://example.com/setup2.exe', 'setup2.exe', '') and
        RobustDownload('https://example.com/setup3.exe', 'setup3.exe', '');
    finally
      DownloadPage.Hide;
    end;
  end
    else Result := True;
end;

enter image description here

Related