如何使用GeckoFX45获取C#上的下载文件百分比?

问题描述 投票:1回答:1

我在C#中使用GeckoFX45,我需要获得下载百分比才能显示进度条。现在我正在使用此代码:

private void LauncherDialog_Download(object sender, LauncherDialogEvent e)
{
    Stream myStream;
    SaveFileDialog saveDialog = new SaveFileDialog();
    saveDialog.Filter = "All files (*.*)|*.*";
    saveDialog.FileName = e.Filename;

    if (saveDialog.ShowDialog() == DialogResult.OK)
    {
        if ((myStream = saveDialog.OpenFile()) != null)
        {
            nsIURI source = IOService.CreateNsIUri(e.Url);
            nsIURI dest = IOService.CreateNsIUri(new Uri(@saveDialog.FileName).AbsoluteUri);
            nsAStringBase t = (nsAStringBase)new nsAString(System.IO.Path.GetFileName(@saveDialog.FileName));
            nsIWebBrowserPersist persist = Xpcom.CreateInstance<nsIWebBrowserPersist>("@mozilla.org/embedding/browser/nsWebBrowserPersist;1");
            nsITransfer nst = Xpcom.CreateInstance<nsITransfer>("@mozilla.org/transfer;1");
            nst.Init(source, dest, t, e.Mime, 0, null, persist, false);
            if (nst != null)
            {
                persist.SetPersistFlagsAttribute(2 | 32 | 16384);
                persist.SetProgressListenerAttribute((nsIWebProgressListener)nst);
                persist.SaveURI(source, null, null, (uint)Gecko.nsIHttpChannelConsts.REFERRER_POLICY_NO_REFERRER, null, null, (nsISupports)dest, null);
            }
            myStream.Close();
        }
    }
}

任何的想法?

c# download geckofx
1个回答
0
投票

我通过实现自己的nsITransfer来实现它:

nst = new TransferTest();

public class TransferTest : nsITransfer
{
  //[...]
  void nsIWebProgressListener2.OnProgressChange64(nsIWebProgress aWebProgress, nsIRequest aRequest, long aCurSelfProgress, long aMaxSelfProgress, long aCurTotalProgress, long aMaxTotalProgress)
  {
    Debug.WriteLine($"Progress: {aCurSelfProgress} / {aMaxSelfProgress}");
  }
}

在未来的版本中可能会有更好的方法,现在我们必须使用这个已弃用的功能。请参阅有关GeckoFX issue tracker的讨论

© www.soinside.com 2019 - 2024. All rights reserved.