Question

I'm stuck with an issue that I'm gonna try and explain clearly. I'm using a gtkmm webkit browser in a sort of embedded application to access php/html files. The point is that I fail to code a way for this webkit to handle downloads.

Oh, and I'm coding in C++.

The file to download currently looks like below in my HTML code :

<a href='excel/template_users.php'><img src='imgs/excelbis.png'/>Download Excel File</a>

The "template_users.php" file is just a way of forcing the user to download the file (you can also access to the website via a "classic" web browser). It contains only :

<?php
    header("Content-disposition: attachment; filename=template_users.xls");
    header("Content-type: application/vnd.ms-excel:");
    readfile("template_users.xls");
?> 

I've been trying to handle downloads by adding to my IntegratedBrowser class constructor (which instantiate the webkit) a connector to the signal "download-requested" :

gboolean ret = FALSE;
g_signal_connect(webView, "download-requested", G_CALLBACK(&IntegratedBrowser::downloadRequested), &ret);

And the associated method :

gboolean IntegratedBrowser::downloadRequested(WebKitWebView* webView, WebKitDownload *download, gboolean *handled)
{
    const gchar* dest = webkit_download_get_uri(download);
    webkit_download_set_destination_uri(download, dest);
   return FALSE;
}

I should precise that everything runs of course fine when I'm trying to download the file via a "classic web browser". The c++ code compile smoothly, however, nothing happens when I click on the link using the webkit. I would like the file to be downloaded in a location chosen by the user if possible (downloading the file into a specific location is viable as well) when clicking on the link.

Have you got any idea of what I am doing incorrectly ?

Thanks for your answers !

Edit : now my downloadRequested method looks like that :

gboolean IntegratedBrowser::downloadRequested(WebKitWebView* webView, WebKitDownload *download, gboolean *handled)
{
    const gchar* dest = g_strdup_printf("%s", "file:///home/user/test.xls");
    webkit_download_set_destination_uri(download, dest);
    return TRUE;
}
Was it helpful?

Solution

The return FALSE from your IntegratedBrowser::downloadRequested() method cancels the download. return TRUE should start it.

Also, you are getting the URI that you are downloading with webkit_download_get_uri() (e.g. http://my.website.com/myfile.jpeg) and setting that to be the destination URI. Set the destination URI to point to a local file instead (although leaving it unset probably downloads the file to a default location.)

If you want a file selector for choosing where to save the download, use GtkFileChooser and when you are done with it, retrieve a URI using gtk_file_chooser_get_uri().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top