question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:60 (33 by maintainers)

github_iconTop GitHub Comments

1reaction
rbricommented, Feb 10, 2021

Ok, i think i got your point, have to check something here. Please be a bit patient…

0reactions
lrpowellcommented, Mar 21, 2021

repeated tests with March 19 htmlunit - 2.48.0-SNAPSHOT, still white .png file results

Read more comments on GitHub >

github_iconTop Results From Across the Web

Javascript download attribute not working when saving image ...
var save = document.createElement('a'); save.href = fileURL; save.download = fileName; alert(save.download); save.click();.
Read more >
Downloading a File with a Save As Dialog in ASP.NET
This code simply uses the bmp.Save() method which writes a binary stream into the Response.OutputStream which results in the binary data being ...
Read more >
Programmatic file downloads in the browser - LogRocket Blog
The download attribute is used to inform the browser to download the URL instead of navigating to it — hence a prompt shows...
Read more >
PowerShell Invoke-WebRequest – Parse and scrape a web ...
PowerShell's Invoke-WebRequest is a powerful cmdlet that allows you to download, parse, and scrape web pages.
Read more >
Download Files using Web API - codeburst
In this article, I will use a demo Web API application in ASP.NET Core to show you how to transmit files through an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found