利用 Geckofx-60 实现下载
方案1. 根据Url直接下载
private void GeckoDownload(string url, string fileFullName)
{
if (System.IO.File.Exists(fileFullName))
{
System.IO.File.Delete(fileFullName);
}
Stream myStream = System.IO.File.Create(fileFullName);
nsIURI source = IOService.CreateNsIUri(url);
nsIURI dest = IOService.CreateNsIUri(new Uri(fileFullName).AbsoluteUri);
nsAStringBase t = (nsAStringBase)new nsAString(System.IO.Path.GetFileName(fileFullName));
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, null, 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. 弹出对话框提示下载
public MainWindow()
{
InitializeComponent();
Gecko.Xpcom.Initialize("Firefox");
Gecko.GeckoPreferences.User["intl.accept_languages"] = "zh-CN,zh;q=0.9,en;q=0.8";
_GeckoWebBrowser = new Gecko.GeckoWebBrowser();
_GeckoWebBrowser.CreateControl();
_GeckoWebBrowser.NoDefaultContextMenu = true; //禁用右键菜
_GeckoWebBrowser.DocumentCompleted += _GeckoWebBrowser_DocumentCompleted;
WindowsFormsHost.Child = _GeckoWebBrowser;
// 弹出下载对话框
Gecko.LauncherDialog.Download += new EventHandler<LauncherDialogEvent>(LauncherDialog_Download)
}
// 实现弹出对话框触发事件
private void LauncherDialog_Download(object sender, Gecko.LauncherDialogEvent e)
{
nsILocalFileWin objTarget = Xpcom.CreateInstance<Gecko.nsILocalFileWin>("@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() == 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");
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();
}
}
}