1

I use a function http://js.cytoscape.org/#cy.jpg to get the graph in jpg format.

I use it with window.location.assign(cy.jpg());, but it opens the image opens in the same tab.

I want it to download instead of opening in the tab. I guess I have to set content-disposition = attachment or something like that.

Edit

I solved it with

const link = document.createElement('a');
link.download = 'filename.png';
link.href = cy.png();
link.click();

however, it is not compatible in all browsers.

mortensen
  • 1,167
  • 2
  • 13
  • 23
  • It seems this function gives you the Base64 representation. One way to upload it could be to POST the value with enctype="multipart/form-data". – Tom Oct 02 '16 at 06:16
  • See this link https://stackoverflow.com/questions/52738739/export-cytoscape-graph-as-png-image-how-to-put-png-tag-on-cytoscape-graph – user10340258 May 21 '19 at 07:31
  • same code used but it open the file not download,i used the same code but it is not working for me. When i click download it open the file but not download MYCODE ' let a: any = document.createElement('a'); a.href = url; a.download = fileName; document.body.appendChild(a); a.style = 'display: none'; a.click(); a.remove();' – Awais Jul 02 '20 at 06:43

1 Answers1

0

You can set <a> element href attribute to result of cy.png(), set download attribute at <a> element, call click() on <a> element.

Alternatively, you can replace MIME type "image/png" portion of data URI returned by cy.png() with "application/octet-stream", then set location.href to replacement data URI

  var url = cy.png();
  url = url.replace("image/png", "application/octet-stream");
  window.location.href = url;

See also How to download a file without using <a> element with download attribute or a server?

Community
  • 1
  • 1
guest271314
  • 1
  • 15
  • 104
  • 177