【问题标题】:How to download doc and open file in Webkit GTK View?如何在 Webkit GTK View 中下载文档并打开文件?
【发布时间】:2013-07-08 21:39:27
【问题描述】:

我目前正在 webkit gtk 视图中运行一个 html 文件。我设置了这些设置:

    let new_settings = new WebKit.WebSettings ();
    new_settings.enable_universal_access_from_file_uris = true;
    this._web_view.set_settings(new_settings);

认为他们会让我在我的计算机上下载一个文件(这并不是我想要做的,但我想对其进行测试)。这不起作用:/

负责的html如下:

<a href="resume/resume1.doc"><img class="shadow" src="images/design/1.jpg" alt="img01"></a>

我要做的是在用户单击图像时自动在 libre office 中打开 resume1.doc。我不太确定如何使用 GTK/HTML 来做到这一点

谢谢! :)

【问题讨论】:

    标签: javascript html webkit gtk webkitgtk


    【解决方案1】:

    不清楚页面是从服务器提供还是在本地加载。

    我没有对本地文件执行此操作,但是对于服务器提供的页面,您将监控 mime 类型决策并向 webkit 指示它需要下载它无法处理的 mime 类型(甚至对于 mime 类型)如果您想下载网页,它可以处理)。接下来,您将提供一个文件名并监控进度。下载完成后,Webkit 会通知您。允许你这样做的信号是

    • mime-type-policy-decision-requested
    • 已请求下载
    • 通知::状态

    对于本地文件,我不知道上述方法是否可行。如果没有,由于您正在控制页面,因此您可以拥有可以告诉您文件需要打开而不是导航的链接属性。

    一旦获得任一方法的文件路径,您就可以使用 xdg-open 命令或其等效功能在可以处理文件的应用程序中打开文件。

    【讨论】:

      【解决方案2】:

      你的开头是对的。您只需要处理 mime-type 并决定您希望如何打开 Libre Office。这是本地文件的示例(uri 是您要在本地打开的服务器上特定文档的路径):

      this._web_view.connect('mime-type-policy-decision-requested',
              (function (webview, frame, request, mimetype, decision) {
                  if (mimetype === 'application/msword' ||
                      mimetype === 'application/vnd.oasis.opendocument.spreadsheet') {
                      // Spawn a libreoffice process with this uri. Necessary because
                      // we want to open the files as templates - the `-n` option
                      // requires the user to save-as.
                      GLib.spawn_async(null, /* cwd */
                                       ['libreoffice', '-n', request.get_uri()],
                                       null, /* inherit environment */
                                       GLib.SpawnFlags.DO_NOT_REAP_CHILD | GLib.SpawnFlags.SEARCH_PATH,
                                       null /* setup function */ );
                      decision.ignore();
                      return true;
      
                  } else if (mimetype === 'application/pdf') {
                      // if PDF, use the build in viewer (usually evince)
                      Gtk.show_uri(null, request.get_uri(), 0);
                      decision.ignore();
                      return true;
                  }
                  // default handler
                  return false;
          }).bind(this));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-05-03
        • 2017-03-26
        • 1970-01-01
        • 1970-01-01
        • 2011-10-22
        • 2011-11-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多