Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 944
Pablo Andrés Martínez Vargas Avatar asked Mar 30 '12 00:03

Pablo Andrés Martínez Vargas


1 Answers

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 java.net 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.

like image 89
jewelsea Avatar answered Oct 12 '22 08:10

jewelsea