C Web浏览器下载文件

时间:2015-07-18 07:10:04

标签: c linux webkitgtk

WebkitGTK+ API Reference

我要做的是在Linux上运行我的HTML5应用程序,我和我的用户仍可以使用我的应用程序,而无需依赖互联网连接。

我的问题是当我去下载时说一个zip文件。下载不会执行,因为没有足够的URL来保存文件(如桌面)。因此它不下载。

因此我的问题依赖于当我通过JSZip动态执行时,我想如何获得足够的URL来下载该文件。 (它在Chrome中运行良好,只是不在我的应用程序中)。终端说......

  

source.c:35:3:警告:在函数返回时使用值'返回'   void [默认启用]返回TRUE; ^

这是我的代码:

/*
  Save this file as main.c and compile it using this command
  (those are backticks, not single quotes):
    gcc -Wall -g -o source source.c `pkg-config --cflags --libs gtk+-2.0 webkit-1.0` -export-dynamic

  Then execute it using:
  ./source

  If you can't compile chances are you don't have gcc installed.
  Install gcc/c with the following terminal command. (This command is for Debian based Linux distros)
    sudo apt-get install libgtk2.0-dev libgtk2.0-doc libglib2.0-doc

  WebKit requires libraries to successfully aquire, configure, and compile. You can get libraries by issuing the following command in your terminal:
    sudo apt-get install subversion gtk-doc-tools autoconf automake libtool libgtk2.0-dev libpango1.0-dev libicu-dev libxslt-dev libsoup2.4-dev libsqlite3-dev gperf bison flex libjpeg62-dev libpng12-dev libxt-dev autotools-dev libgstreamer-plugins-base0.10-dev libenchant-dev libgail-dev

  WebkitGTK+ API Reference: http://webkitgtk.org/reference/webkitgtk/stable/webkitgtk-webkitdownload.html

  Ubuntu Webkit information - https://help.ubuntu.com/community/WebKit
    sudo apt-get install libwebkitgtk-dev python-webkit-dev python-webkit

  Required dependencies for this build: (If you installed all the above this is not needed)
    sudo apt-get install libgtk2.0-dev libgtk2.0-doc libglib2.0-doc subversion gtk-doc-tools autoconf automake libtool libgtk2.0-dev libpango1.0-dev libicu-dev libxslt-dev libsoup2.4-dev libsqlite3-dev gperf bison flex libjpeg62-dev libpng12-dev libxt-dev autotools-dev libgstreamer-plugins-base0.10-dev libenchant-dev libgail-dev libwebkitgtk-dev
*/

#include <gtk/gtk.h>
#include <webkit/webkit.h>

static void destroy_cb(GtkWidget* widget, gpointer data) {
  gtk_main_quit();
}

static void download_requested_cb(WebKitWebView *web_view, WebKitDownload *download) {
  const gchar* dest = g_strdup_printf("%s", "file:///home/michael/Downloads/test.zip");
  //set the destination uri (eg, send the file to a downloads folder)
  webkit_download_set_destination_uri(download, dest);
  webkit_download_start();

  return TRUE;
}

int main(int argc, char* argv[]) {
  GtkWidget* window;
  WebKitWebView* web_view;

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_widget_set_name (window, "AppName");
  gtk_window_set_default_size(GTK_WINDOW(window), 600, 600);
  //gtk_window_set_icon_from_file(window, "app/logo.png", NULL);
  g_signal_connect(window, "destroy", G_CALLBACK(destroy_cb), NULL);

  web_view = WEBKIT_WEB_VIEW(webkit_web_view_new());

  /* Register a callback that gets invoked each time a download is requested */
  g_object_connect(web_view, "download-requested", G_CALLBACK(download_requested_cb), NULL);

  char uri[PATH_MAX];
  char cwd[PATH_MAX];

  getcwd(cwd, sizeof(cwd));

  if (argc > 1)
      snprintf(uri, sizeof(uri), "%s", argv[1]);
  else
      snprintf(uri, sizeof(uri), "file://%s/app/index.html", cwd);

  webkit_web_view_open (web_view, uri);

  gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(web_view));
  gtk_widget_grab_focus(GTK_WIDGET(web_view));
  gtk_widget_show_all(window);
  gtk_main();
  return 0;
}

1 个答案:

答案 0 :(得分:0)

1)你的基本前提是健全的:

file:///home/michael/Downloads/test.zip //此语法应该允许您在硬盘上“上传”本地文件

2)这是编译警告(不是错误)。从理论上讲,你可以忽略它:

source.c:35:3: warning: ‘return’ with a value, in function returning void [enabled by default] return TRUE; ^

3)这就是问题所在:

static void download_requested_cb(WebKitWebView *web_view, ... 
  ...    
  return TRUE; // Delete this line.  You can't return anything from a "void" function!
}