如何在GeckoFX 29中处理下载

时间:2014-12-08 23:15:27

标签: c# .net gecko xulrunner geckofx

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

我试过这个处理程序

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

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

3 个答案:

答案 0 :(得分:5)

之后的表单加载中
browser.navigate("http://www.domain.com");

使用此:

LauncherDialog.Download += LauncherDialog_Download;

创建LauncherDialog_Download

void LauncherDialog_Download(object sender, LauncherDialogEvent e)
    {
        nsILocalFile objTarget = Xpcom.CreateInstance<nsILocalFile>("@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<nsIWebBrowserPersist>("@mozilla.org/embedding/browser/nsWebBrowserPersist;1");
                nsIDownloadManager DownloadMan = null;
                DownloadMan = Xpcom.CreateInstance<nsIDownloadManager>("@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 from here.

和Xulrunner from here.

答案 1 :(得分:3)

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

    private void LauncherDialog_Download(object sender, Gecko.LauncherDialogEvent e)
    {
        nsILocalFile objTarget = Xpcom.CreateInstance<nsILocalFile>("@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<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();
            }
        }
    }

答案 2 :(得分:2)

另一种方式:

 private void LauncherDialog_Download(object sender, Gecko.LauncherDialogEvent e)
 {
     SaveFileDialog saveFileDialog1 = new SaveFileDialog();

     saveFileDialog1.Filter = "All files (*.*)|*.*";
     saveFileDialog1.FilterIndex = 2;
     saveFileDialog1.RestoreDirectory = true;
     saveFileDialog1.FileName = e.Filename;

     if (saveFileDialog1.ShowDialog() == DialogResult.OK)
     {
         nsILocalFile objTarget = Xpcom.CreateInstance<nsILocalFile>("@mozilla.org/file/local;1");

         using(nsAString tmp = new nsAString(saveFileDialog1.FileName))
         {
             objTarget.InitWithPath(tmp);
         }
         e.HelperAppLauncher.SaveToDisk(objTarget, false);
     }
}