C Web 浏览器下载文件

标签 c linux webkitgtk

WebkitGTK+ API Reference

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

我的问题是当我去下载一个 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//此语法应允许您“上传”硬盘上的本地文件

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

关于C Web 浏览器下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31488767/

相关文章:

c - 如何判断c中是否有换行符或数字?

linux - FFMPEG Bash 自动化

WebKitGTK +,GTK2,GTK3

python - 使用 WebKitGTK+ 等待网站完全加载

c - 从用户那里获取输入 - 不起作用

C 程序从 n 运行循环直到 1

c - 如何用表格制作 "global variable"?

linux - 尝试部署 Firebase 功能时找不到模块 'firebase-admin'

linux - 如何使用 ptrace(2) 改变系统调用的行为?

c - webkit_dom_element_get_elements_by_tag_name 无法正常工作