How to handle downloading in GeckoFX 29

后端 未结 3 2152
你的背包
你的背包 2020-12-20 02:16

How can I handle downloading in GeckoFx I\'m using version 29 I\'ve found some ways like adding event of
LauncherDialog_Download(

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-20 02:47

    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("@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("@mozilla.org/embedding/browser/nsWebBrowserPersist;1");
                    nsIDownloadManager DownloadMan = null;
                    DownloadMan = Xpcom.CreateInstance("@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.

提交回复
热议问题