kinimesi / cytoscape-svg

A Cytoscape.js extension to export the current graph view as an SVG.
https://kinimesi.github.io/cytoscape-svg
GNU General Public License v3.0
35 stars 5 forks source link

Cannot download .svg picture #10

Closed jonng1000 closed 2 years ago

jonng1000 commented 2 years ago

Hi I tried using the cytoscape-svg library to download my network in .svg format but it didnt work. I had the below error: image

Below is the code which I have:

     const downloadPng = (cyToDownload, picFormat) => {
        if (picFormat == 'png') {
          const png = cyToDownload.png()
          var a = document.createElement("a"); //Create <a>
          a.href = png
          a.download = "local_network.png"; //File name Here
          a.click(); //Download file
        } 

        else if (picFormat == 'jpg') {
        const jpg = cyToDownload.jpg()
        var a = document.createElement("a"); //Create <a>
        a.href = jpg
        a.download = "local_network.jpg"; //File name Here
        a.click(); //Download file
        } 

        else if (picFormat == 'svg') {
        console.log("download func called")
        const svgPic = cyToDownload.svg()
        console.log(svgPic)
        var a = document.createElement("a"); //Create <a>
        a.href = svgPic
        a.download = "local_network.svg"; //File name Here
        a.click(); //Download file
        }
  }

So my functions to download my network in png and jpg work, so I don't know why it can't download the network in .svg format. Could I get some help please?

Thank you Jonathan

kinimesi commented 2 years ago

Hi, for SVG try the following:

a.href='data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svgPic);
jonng1000 commented 2 years ago

Ok thanks so much =)