Skip to content

With Javascript - no chart png file from "download" attribute click, or by saving result from WebResponse #305

@lrpowell

Description

@lrpowell

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:
Waits_Overview

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:

  1. why isn't the download created when running in HtmlUnit? Is this supposed to work (the above code where link2 is created and clicked)?
    _

link2.setAttribute("download", fName + ".png");
link2.click();

_

  1. if it doesn't work to automatically do the download, how do I need to process the results from HtmlUnit to get the png image in a file?
    Right now I am getting a "blank screen" with this code:
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();
}

testFile2

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions