I have a chart that I am downloading with Javascript. I do not need to click a button from HtmlUnit because I am running through the Javascript that will do the download:
var imgData2 = canvas.toDataURL('image/png');
imgData2 = imgData2.replace('image/png', 'image/octet-stream');
var link2 = document.createElement("a");
link2.setAttribute("href", imgData2);
link2.setAttribute("download", fName + ".png");
link2.click();
When I launch to this directly, I can both view the live chart and I get a download of the static chart in my browser downloads file. URL:
http://a405bp3.rch.stglabs.ibm.com:2004/SystemiNavigator/com/ibm/as400/pt/dojoChart/DojoGraphScriptLauncher.html?packageId=IBM_CS&perspectiveId=P_WAITS_OVERVIEW&perspectiveName=Waits Overview&collectionName=Q035000004&collectionLib=QPFRDATA&collectionType=Collection Services File Based Collection&fileLevel=48&startTime=Feb 3, 2021 11:00:05 PM&endTime=Ongoing&systemName=A405BP3 &release=V7R3M0&collectionFormat=*CSFILE&basePerspId=
Resulting png file:
I want to duplicate this with HtmlUnit, but may be missing something in how this should be done.
The response header type is the image/png as expected (or image/octet-stream if I do the replace as above)
[content-type=image/png;charset=US-ASCII]
The content string looks like it is the png data: content string:�PNG
� ....
I am wondering 2 things:
link2.setAttribute("download", fName + ".png");
link2.click();
_
try (final WebClient webClient = new WebClient(BrowserVersion.FIREFOX)) {
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(java.util.logging.Level.OFF);
webClient.getCache().clear();
webClient.getOptions().setCssEnabled(true);
webClient.getOptions().setJavaScriptEnabled(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.getOptions().setThrowExceptionOnScriptError(true);
Page page = webClient.getPage(url);
webClient.waitForBackgroundJavaScriptStartingBefore(10_000);
// Tried this with the same results:
//Page page2 = page.getEnclosingWindow().getEnclosedPage();
//WebResponse response = page2.getWebResponse();
WebResponse response = page.getWebResponse();
try {
IOUtils.copy(response.getContentAsStream(), outStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outStream != null) {
try {
outStream.close();
}
catch (IOException e) {
}
}
}
webClient.close();
}
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too