Firefox / Gecko嵌入式浏览器

时间:2016-11-04 20:02:28

标签: firefox gecko geckofx

我使用Gecko嵌入式浏览器,在最后一步,我点击链接并下载一个zip文件。除了浏览器使用默认程序打开文件外,它运行良好。如何阻止浏览器打开下载的zip文件?

1 个答案:

答案 0 :(得分:0)

您需要订阅LauncherDialog事件并提供自己的实现:

原始答案在这里: https://bitbucket.org/geckofx/geckofx-45.0/issues/15/downloading-files-with-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();
        }
    }
}