How to handle file downloads using JavaFX 2.0 WebEngine


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.

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

3/30/2012 12:01:16 AM

Accepted Answer

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. Edit: This blog page does not exist anymore. Here is the latest archive of this blog page.

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 url connection handler.

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.

5/29/2015 5:17:53 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow