How do I handle file downloads in GeckoFX?

2019-05-28 23:29发布

问题:

I am using the latest GeckoFX 18 (hindlemail's fork) and have tried hard to achieve this simple method : Handle file downloads.

I want to know if there is a file download happening in the GeckoWebBrowser. There is no file download event, and even worse : clicking a link that leads to a file download doesn't trigger /any/ event. It just doesn't do nothing. No download dialog, no save file dialog, no url, no nothing.

Is there a way I can handle file downloads ?

回答1:

By using hindlemail's fork of geckofx you will have to handle LauncherDialog.Download event. This event has several parameters like url, filename, etc.

LauncherDialog.Download += LauncherDialog_Download;
////
void LauncherDialog_Download(object sender, LauncherDialogEvent e)
{
    string filename = e.Filename; //do something with filename
    string url = e.Url; //use webclient to download file from this url
}

Even with this you will not be able to download files from secure sites like dropbox or facebook but it will download something, better than nothing. I don't know much about xul so I also has a hard time downloading files.

I tried this too:

void LauncherDialog_Download(object sender, LauncherDialogEvent e)
{
    WebBrowser ie = new WebBrowser();
    ie.Navigate(e.Url);
}

It will show Internet Explorer download file dialog if file can be downloaded that way. Probably cause of request headers or something. I also used Fiddler to find out what headers Firefox sends to server but I found nothing useful.