如何在GeckoFX 29中处理下载 [英] How to handle downloading in GeckoFX 29
问题描述
我如何在 GeckoFx 中处理下载,我正在使用版本 29 我发现了一些类似添加
事件的方法 LauncherDialog_Download(对象发送者,LauncherDialogEvent e)
但是,我无法为此事件添加处理程序
How can I handle downloading in GeckoFx I'm using version 29
I've found some ways like adding event of
LauncherDialog_Download(object sender, LauncherDialogEvent e)
But, I'm not able to add handler for this event
我为处理程序尝试过
LauncherDialogFactory.Register();
LauncherDialog.Download += LauncherDialog_Download;
但是,它显示为错误,我该如何添加处理程序
还有其他方法来处理 GeckoFx 29 中的下载吗?
But, it is showing as error, how can i add handler
and is there any other ways to handle downloading in GeckoFx 29?
推荐答案
表单加载后
browser.navigate("http://www.domain.com");
使用此:
LauncherDialog.Download += LauncherDialog_Download;
创建LauncherDialog_Download
Create 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,因此您的程序将询问您要将文件保存在何处.
The above code also triggers saveFileDialog so your program will ask where you want to save the file.
经过测试,可以在GeckoFX 31和33上使用,但也应该在29上使用.如果没有,请从此处下载最新的GeckoFX .
Tested and working with GeckoFX 31 and 33 but should work in 29 as well. If it doesn't, download the latest GeckoFX from here.
和Xulrunner从这里.
And Xulrunner from here.
这篇关于如何在GeckoFX 29中处理下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!