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

标签 download inno-setup pascalscript

我的 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 文档,但没有找到任何可以启用该功能的标志。有解决办法吗?提前致谢。

最佳答案

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

以下代码将允许用户选择对每个文件的下载错误执行的操作:

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

关于download - Inno Setup 出错时如何继续下载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71659973/

相关文章:

powershell - 如何在 PowerShell 中从 Web 下载整个文件夹/子文件夹

ios - 将文件下载到设备时 iPad 应用程序崩溃

inno-setup - InnoSetup - 编译时出现错误 "File in use by another process..."消息

inno-setup - 在 Inno Setup 中使用 SAPI 对象

inno-setup - 在 Inno Setup 中使用字体大小缩放单选按钮列表

Swift - 自动将 pdf 文件保存到我 iPhone 上的文件应用程序

Android - 从下载的资源中获取 Widget RemoteView setImageViewBitmap

installation - 通过Inno Setup创建的安装程序无法在Windows 10上的安装过程中关闭应用程序

macros - 在 Inno Setup [代码] 部分将宏扩展为 Pascal 代码

regex - Inno Setup 中字符串的正则表达式