如何在GeckoFX 29中处理下载

如何在GeckoFx中处理下载我正在使用版本29我发现了一些方法,比如添加事件
LauncherDialog_Download(object sender, LauncherDialogEvent e)但是,我无法为此事件添加处理程序

我试过这个处理程序

 LauncherDialogFactory.Register(); LauncherDialog.Download += LauncherDialog_Download; 

但是,它显示为错误,我如何添加处理程序
还有其他方法可以处理GeckoFx 29中的下载吗?

在你的表格加载后

 browser.navigate("http://www.domain.com"); 

用这个:

 LauncherDialog.Download += LauncherDialog_Download; 

创建LauncherDialog_Download

 void LauncherDialog_Download(object sender, LauncherDialogEvent e) { nsILocalFile objTarget = Xpcom.CreateInstance("@mozilla.org/file/local;1"); using (nsAString tmp = new nsAString(@Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\temp.tmp")) { objTarget.InitWithPath(tmp); } //Save file dialog Stream myStream; SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "All files (*.*)|*.*"; saveFileDialog1.FilterIndex = 2; saveFileDialog1.RestoreDirectory = true; saveFileDialog1.FileName = e.Filename; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { if ((myStream = saveFileDialog1.OpenFile()) != null) { nsIURI source = IOService.CreateNsIUri(e.Url); nsIURI dest = IOService.CreateNsIUri(new Uri(@saveFileDialog1.FileName).AbsoluteUri); nsAStringBase t = (nsAStringBase)new nsAString(System.IO.Path.GetFileName(@saveFileDialog1.FileName)); nsIWebBrowserPersist persist = Xpcom.CreateInstance("@mozilla.org/embedding/browser/nsWebBrowserPersist;1"); nsIDownloadManager DownloadMan = null; DownloadMan = Xpcom.CreateInstance("@mozilla.org/download-manager;1"); nsIDownload download = DownloadMan.AddDownload(0, source, dest, t, e.Mime, 0, null, (nsICancelable)persist, false); if (download != null) { persist.SetPersistFlagsAttribute(2 | 32 | 16384); persist.SetProgressListenerAttribute((nsIWebProgressListener)download); persist.SaveURI(source, null, null, null, null, (nsISupports)dest, null); } myStream.Close(); } } } 

上面的代码也会触发saveFileDialog,因此您的程序会询问您要保存文件的位置。

测试并使用GeckoFX 31和33,但也应该在29中工作。 如果没有,请从此处下载最新的GeckoFX 。

和Xulrunner 来自这里。

Jonathan的解决方案不适用于geckoFX 45 – 似乎他们改变了一些库并弃用了下载管理器。 以下是与GeckoFX 45一起更新的相同事件片段

  private void LauncherDialog_Download(object sender, Gecko.LauncherDialogEvent e) { nsILocalFile objTarget = Xpcom.CreateInstance("@mozilla.org/file/local;1"); using (nsAString tmp = new nsAString(@Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\temp.tmp")) { objTarget.InitWithPath(tmp); } //Save file dialog Stream myStream; SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "All files (*.*)|*.*"; saveFileDialog1.FilterIndex = 2; saveFileDialog1.RestoreDirectory = true; saveFileDialog1.FileName = e.Filename; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { if ((myStream = saveFileDialog1.OpenFile()) != null) { nsIURI source = IOService.CreateNsIUri(e.Url); nsIURI dest = IOService.CreateNsIUri(new Uri(@saveFileDialog1.FileName).AbsoluteUri); nsAStringBase t = (nsAStringBase)new nsAString(System.IO.Path.GetFileName(@saveFileDialog1.FileName)); nsIWebBrowserPersist persist = Xpcom.CreateInstance("@mozilla.org/embedding/browser/nsWebBrowserPersist;1"); nsITransfer nst = Xpcom.CreateInstance("@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(); } } }