Save variable to SVG and PNG files (Base64)?

  • 0
    Help me with the problem. In my application, images are generated in svg and png format. I drove them into variables. And here's how to save to files from variables ... Found on the stack for .TXT.
    document.getElementsByTagName('a')[0].onclick = function() {
       var text = "text";
        var csvData = 'data:application/txt;charset=utf-8,' + encodeURIComponent(text);
        this.href = csvData; = '_blank'; = 'txt.txt';

    I tried to fit it for myself, but the files are saved with an error.
    document.getElementById('downloadSVG').onclick = function() {
            let barcodeSVG = $('svg');
            let barName = $("#userInput").val();
            let data = 'data:image/svg+xml; charset=utf8,' + encodeURIComponent(barcodeSVG);
            this.href = data;
   = 'Штрихкод ' + barName + '.svg';

    Tried through new Blob () then files are just empty even without SVg headers
    JavaScript Natalie Rich, Aug 15, 2019

  • 0 Answers
Your Answer
To place the code, please use CodePen or similar tool. Thanks you!