C Web 浏览器下载文件

C Web Browser Download File

WebkitGTK+ API Reference

我想做的是 运行 我的 HTML5 应用 Linux 这样我和我的用户仍然可以在不依赖互联网连接的情况下使用我的应用。

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

因此,我的问题在于,当通过 JSZip 动态执行文件时,我应该如何获得足够的 url 来下载该文件。 (它在 Chrome 中工作正常,只是在我的应用程序中没有)。终端机说...

source.c:35:3: warning: ‘return’ with a value, in function returning void [enabled by default] return 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) 你的基本前提是正确的:

file:///home/michael/Downloads/test.zip // 此语法应允许您 "upload" 硬盘上的本地文件

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!
}