Button for downloading SVG in JavaScript & HTML? - Stack Overflow

There's an SVG image that's rendered in the browser. I want a button below to download the SV

There's an SVG image that's rendered in the browser. I want a button below to download the SVG. Looks like download with proper mimetype is the way to go.

Attempt:

<div id="container"></div>
<button id="download">Download SVG</button>
function downloadSVG() {
    const svg = document.getElementById('container').innerHTML;
    /*console.info(btoa(svg));

    document.getElementById('svg').src = `data:image/svg+xml;utf8,${document.createTextNode(svg).textContent}`;
    console.info('src:', document.getElementById('svg').src, ';');*/

    const element = document.createElement('a');
    const mimeType = 'image/svg+xml'; // 'image/svg+xml;utf8';
    element.href = `${mimeType},${document.createTextNode(svg).textContent}`;
    element.target = '_blank';
    element.mimeType = mimeType;
    element.download = 'w3c.svg';
    element.id = 'downloader';
    document.body.appendChild(element);
    element.click();
    document.getElementById('downloader').remove();
}

Runnable example:

But I get a broken SVG file. Similar issue with my real code (I get an empty SVG).

There's an SVG image that's rendered in the browser. I want a button below to download the SVG. Looks like download with proper mimetype is the way to go.

Attempt:

<div id="container"></div>
<button id="download">Download SVG</button>
function downloadSVG() {
    const svg = document.getElementById('container').innerHTML;
    /*console.info(btoa(svg));

    document.getElementById('svg').src = `data:image/svg+xml;utf8,${document.createTextNode(svg).textContent}`;
    console.info('src:', document.getElementById('svg').src, ';');*/

    const element = document.createElement('a');
    const mimeType = 'image/svg+xml'; // 'image/svg+xml;utf8';
    element.href = `${mimeType},${document.createTextNode(svg).textContent}`;
    element.target = '_blank';
    element.mimeType = mimeType;
    element.download = 'w3c.svg';
    element.id = 'downloader';
    document.body.appendChild(element);
    element.click();
    document.getElementById('downloader').remove();
}

Runnable example: https://stackblitz./edit/typescript-mpk8ui

But I get a broken SVG file. Similar issue with my real code (I get an empty SVG).

Share Improve this question asked Sep 5, 2019 at 5:21 A TA T 13.9k23 gold badges107 silver badges168 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 12

The downloading data must be a blob raw data.

function downloadSVG() {
  const svg = document.getElementById('container').innerHTML;
  const blob = new Blob([svg.toString()]);
  const element = document.createElement("a");
  element.download = "w3c.svg";
  element.href = window.URL.createObjectURL(blob);
  element.click();
  element.remove();
}

This should do the trick.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743761761a4502760.html

相关推荐

  • Button for downloading SVG in JavaScript &amp; HTML? - Stack Overflow

    There's an SVG image that's rendered in the browser. I want a button below to download the SV

    6天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信