How to handle file downloads using JavaFX 2.0 WebEngine

When using WebEngine and WebView JavaFX 2.0 to display some HTML content, I cannot handle loading from an HTML page at all. When I click on any downloadable link, nothing happens.

Can I handle downloads in JavaFX 2.0 WebView / WebEngine?

+8
javafx javafx-2
source share
1 answer

WebView does not currently have download functionality. You can implement it yourself by controlling the WebView location property, and then generating the appropriate code to complete 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. } }); 

Sample code for handling downloads from JavaFX can be found in this zenjava blog post . Edit: This blog page no longer exists. Here is the latest archive of this blog page.

Downloads in web browsers are often triggered using http content-type or content-disposition and can be based on matching mime-type / file extensions. The above scheme will only work for matching file extensions where the file extension comes from a location. To handle downloads based on a content or content header, you probably need to implement your own java.net URL connection handler.

To implement this functionality in the core JavaFX libraries, you can test Jira's JavaFX to request a function around this and, if it does not exist, create a new function request.

+13
source share

All Articles