0

I want to download a file from a website. I have already tested my adapted download code on a Html-Form leading directly to the file, see http://rapgru.lima-city.de/fcmanager/download.html

But if i press the download button on the website where I want the file to be downloaded, nothing happens. Not even the URL gets changed... I have already viewed the site's code, and found out that a form, which action is the same site as I'm currently on, gets submitted. I think it wants to perform a JavaScript download which can't be done by the JavaFX WebView.

My current code checks after every URL change if the URL is a downloadable file, and if yes, he downloads it:

web_view.getEngine().locationProperty().addListener(new ChangeListener<String>() {
              @Override public void changed(ObservableValue<? extends String> observableValue, String oldLoc, String newLoc) {
                String downloadableExtension = null;  // todo I wonder how to find out from WebView which documents it could not process so that I could trigger a save as for them?
                String[] downloadableExtensions = { ".csv", ".csv?"};
                logger.info("Site changed to " + newLoc);
                for (String ext: downloadableExtensions) {
                  if (newLoc.endsWith(ext)) {
                    downloadableExtension = ext;
                    if(downloadableExtension.equals(".csv?"))
                    {
                        downloadableExtension = ".csv";
                    }
                    logger.info("File is " + downloadableExtension);
                    break;
                  }
                }
                if (downloadableExtension != null) {  
                    logger.info("Starting download process for " + newLoc);
                  // create a file save option for performing a download.
                  int filenameIdx = newLoc.lastIndexOf("/") + 1;
                  if (filenameIdx != 0) {
                    File saveFile = new File("data/lastdownload" + downloadableExtension);

                    if (saveFile != null) {
                      BufferedInputStream  is = null;
                      BufferedOutputStream os = null;
                      try {
                        is = new BufferedInputStream(new URL(newLoc).openStream());
                        os = new BufferedOutputStream(new FileOutputStream(saveFile));
                        int b = is.read();
                        while (b != -1) {
                          os.write(b);
                          b = is.read();
                        }
                      } catch (FileNotFoundException e) {
                        System.out.println("Unable to save file: " + e);
                      } catch (MalformedURLException e) {
                        System.out.println("Unable to save file: " + e);
                      } catch (IOException e) {
                        System.out.println("Unable to save file: " + e);
                      } finally {
                        try { if (is != null) is.close(); } catch (IOException e) { /** no action required. */ }
                        try { if (os != null) os.close(); } catch (IOException e) { /** no action required. */ }
                      }
                    }

EDIT: The page where the actual Download-Button is, is not my website! http://rapgru.lima-city.de/fcmanager/download.html is my website where I tested the code above

rapgru
  • 368
  • 2
  • 9
  • If the code is relevant to your question, you should include it in the question instead of just linking it. For debugging such issues I suggest the inclusion of Firebug in the `WebView`. See http://stackoverflow.com/questions/9398879/html-javascript-debugging-in-javafx-webview – hotzst May 22 '16 at 15:49
  • I think it's not relevant, because the URL Change Event doesn't even get fired when clicking the button, but see my edit above. – rapgru May 22 '16 at 18:13

0 Answers0