How to handle downloading in GeckoFX 29

匆匆过客 提交于 2019-12-29 08:12:15

问题


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

I tried this for handler

LauncherDialogFactory.Register();
LauncherDialog.Download += LauncherDialog_Download;

But, it is showing as error, how can i add handler
and is there any other ways to handle downloading in GeckoFx 29?


回答1:


In form load after your

browser.navigate("http://www.domain.com");

Use this:

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();
            }
        }
    }

The above code also triggers saveFileDialog so your program will ask where you want to save the file.

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.

And Xulrunner from here.




回答2:


Jonathan's solution doesn't work with geckoFX 45 - it seems they changed some of the libraries and deprecated the download manager. Here is the same event snippet updated to work with GeckoFX 45

    private void LauncherDialog_Download(object sender, Gecko.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");

                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();
            }
        }
    }



回答3:


Another way:

 private void LauncherDialog_Download(object sender, Gecko.LauncherDialogEvent e)
 {
     SaveFileDialog saveFileDialog1 = new SaveFileDialog();

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

     if (saveFileDialog1.ShowDialog() == DialogResult.OK)
     {
         nsILocalFile objTarget = Xpcom.CreateInstance<nsILocalFile>("@mozilla.org/file/local;1");

         using(nsAString tmp = new nsAString(saveFileDialog1.FileName))
         {
             objTarget.InitWithPath(tmp);
         }
         e.HelperAppLauncher.SaveToDisk(objTarget, false);
     }
}


来源:https://stackoverflow.com/questions/27368791/how-to-handle-downloading-in-geckofx-29

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!