c#geckofx文件流下载

备注:内容仅提供参考。

⒈添加引用:using Gecko;

⒉然后根据自己的情况在某个方法内添加事件:

        LauncherDialog.Download += new EventHandler<LauncherDialogEvent>(OnDownloadFile);

⒊再声明方法:

private void OnDownloadFile(object sender, LauncherDialogEvent e)
        {
            nsILocalFile objTarget = Xpcom.CreateInstance<nsILocalFile>("@mozilla.org/file/local;1");

            using (nsAString tmp = new nsAString(@Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "	emp.tmp"))
            {
                objTarget.InitWithPath(tmp);
            }

            //Save file dialog
            Stream myStream;
            System.Windows.Forms.SaveFileDialog saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();

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

            if (saveFileDialog1.ShowDialog() == System.Windows.Forms.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");
                    persist.SaveURI(source, null, null, null, null, (nsISupports)dest, null);
                    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();
                }
            }
        }
原文地址:https://www.cnblogs.com/RedSky/p/5626006.html