
正文
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) + "\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 = ;
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(, source, dest, t, e.Mime, , null, (nsICancelable)persist, false); if (download != null)
{
persist.SetPersistFlagsAttribute( | | );
persist.SetProgressListenerAttribute((nsIWebProgressListener)download);
persist.SaveURI(source, null, null, null, null, (nsISupports)dest, null);
} myStream.Close();
}
}
}








