如何使用JavaFX 2.0 WebEngine处理文件下载 [英] How to handle file downloads using JavaFX 2.0 WebEngine

查看:66
本文介绍了如何使用JavaFX 2.0 WebEngine处理文件下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用JavaFX 2.0的WebEngine和WebView显示一些HTML内容时,我根本无法处理从HTML页面进行的下载.当我单击任何可下载的链接时,什么都没有发生.

When using the WebEngine and WebView of JavaFX 2.0 to show some HTML content, I'm not able to handle downloads from the HTML page at all. When I click on any downloadable link nothings happens at all.

是否可以在JavaFX 2.0 WebView/WebEngine中处理下载?

Is it possible to handle downloads in JavaFX 2.0 WebView/WebEngine?

推荐答案

WebView当前未实现下载功能.您可以通过监视WebView的location属性然后创建适当的代码来执行下载来自己实现它.

Download functionality is currently not implemented in WebView. You can implement it yourself by monitoring the location property of the WebView and then creating appropriate code to perform the download.

webView.getEngine().locationProperty().addListener(new ChangeListener<String>() {
  @Override public void changed(ObservableValue<? extends String> observableValue, String oldLoc, String newLoc) {
    // check if the newLoc corresponds to a file you want to be downloadable
    // and if so trigger some code and dialogs to handle the download.
  }
});

在此

An example of code to handle a download from JavaFX can be found in this zenjava blog entry. This blog page does not exist anymore. Here is the latest archive of this blog page.

Web浏览器中的下载通常由http 内容类型内容处置标头,并且可以基于mime类型/文件扩展名映射.以上方案仅适用于文件扩展名映射,其中文件扩展名是从该位置派生的.为了根据内容类型或内容处置标头处理下载,您可能需要实现自己的java.net url连接处理程序.

Downloads in web browsers are often triggered by http content-type or content-disposition headers and can be based upon a mime-type/file extension mapping. The above scheme will only work for a file extension mapping where the file extension is derived from the location. For handling downloads based on a content-type or content-disposition header you would likely need to implement your own java.net url connection handler.

要在核心JavaFX库中实现此功能,可以检查 JavaFX Jira 的功能围绕此请求,如果不存在,则创建一个新的功能请求.

To get this functionality implemented in the core JavaFX libraries you could check the JavaFX Jira for a feature request around this and, if it is not there, create a new feature request.

这篇关于如何使用JavaFX 2.0 WebEngine处理文件下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆