0

I have a WebKitWebView. On a website there is a download requested. I don't know how to write the signal download-requested that the download starts and saves to a given directory. I use Ubuntu 12.04 LTS with Anjuta. I'm programming in C.

user1464420
  • 89
  • 1
  • 4

2 Answers2

2
  1. Connect the signal:

    gboolean ret = FALSE;
    g_signal_connect(webView, "download-requested", G_CALLBACK(downloadRequested), &ret);
    
  2. Write the signal handler:

    static gboolean downloadRequested(WebKitWebView* webView, WebKitDownload *download,     gboolean *handled)
    {
        const gchar* dest = g_strdup_printf("%s", "file://xxx"); // The 'dest' path should be customized path using 'webkit_download_get_uri'
        webkit_download_set_destination_uri(download, dest);
        return TRUE;
    }
    

    If you want to handle the download process yourself, you should return FALSE; here.

Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
  • it did not work. I used static gboolean downloadRequested(WebKitWebView *web_view, WebKitDownload *download, gboolean handled) { const gchar *dest = g_strdup_printf("%s", "/home/malteuser/Downloads/test.mp3"); // The 'dest' p ath should be customized path using 'webkit_download_get_uri' webkit_download_set_destination_uri(download, dest); return TRUE; } – user1464420 Jul 07 '12 at 12:43
  • 1
    The file isn't created. I get this error: (program:3947): GLib-GIO-CRITICAL **: g_output_stream_write_all: assertion `G_IS_OUTPUT_STREAM (stream)' failed – user1464420 Jul 07 '12 at 12:45
  • You should use "file:///home/malteuser/Downloads/test.mp3". Pls have a try. – Shen Weizheng Jul 07 '12 at 23:13
-2

He forgot to start the download !

static gboolean downloadRequested(WebKitWebView* webView, WebKitDownload *download, gboolean *handled)
{
  const gchar* dest = g_strdup_printf("%s", "file:///home/administrator/Downloads/test.jpg");
  webkit_download_set_destination_uri(download, dest);
  webkit_download_start(download);  //start the download
  return TRUE;
}
sonichy
  • 1,370
  • 2
  • 14
  • 17