您的位置:首页 > 编程语言 > C#

c#geckofx文件流下载

2016-06-29 10:41 489 查看
备注:内容仅提供参考。

⒈添加引用: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) + "\temp.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();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: