首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Firefox/Gecko嵌入式浏览器

Firefox/Gecko嵌入式浏览器
EN

Stack Overflow用户
提问于 2016-11-05 04:02:28
回答 1查看 721关注 0票数 1

我使用Gecko嵌入式浏览器,在最后一步,我单击链接并下载一个zip文件。它工作得很好,除了浏览器用默认程序打开文件。如何防止浏览器打开下载的zip文件?

EN

回答 1

Stack Overflow用户

发布于 2017-05-10 04:08:21

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

原始答案在这里:https://bitbucket.org/geckofx/geckofx-45.0/issues/15/downloading-files-with-geckofx-45

发布代码以供将来参考:

代码语言:javascript
复制
 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();
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40430903

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档