如何在 Inno Setup 出错时继续下载?

How to continue download on error in Inno Setup?

我的 Inno Setup 脚本使用内置功能下载一些资源。 它创建下载向导页面:

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

它添加了几个应该下载的项目:

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;

最后一步是显示向导页面并开始下载:

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

以上基本上都是Inno Setup提供的例子。有一个问题:如果任何下载失败(抛出异常或任何东西),它会中断整个下载过程,其他项目将不会被下载。我希望它继续下载其余文件。我浏览了 Inno Setup 文档,但没有找到任何可以启用它的标志。有解决办法吗?提前致谢。

一个简单的解决方案是单独下载每个文件。

下面的代码将允许用户select如何处理每个文件的下载错误:

  • 重试下载
  • 跳过下载
  • 中止安装。
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;