Created
April 14, 2016 17:32
-
-
Save aosalias/1f0dd03b2f7d639fee58dd090b9a519b to your computer and use it in GitHub Desktop.
Create a download link for an svg client side (Like one drawn from D3.js) (Excuse the jQuery, this could easily be done in raw js)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('.container svg').attr({ | |
'xmlns': 'http://www.w3.org/2000/svg', | |
'xmlns:xlink': "http://www.w3.org/1999/xlink" | |
}); | |
var base64SvgText = btoa( | |
encodeURIComponent( | |
$('.container').html() | |
).replace(/%([0-9A-F]{2})/g, | |
function (match, p1) { | |
return String.fromCharCode('0x' + p1); | |
} | |
) | |
); | |
$('a.download-svg').attr({ | |
'download': 'cool-graph.svg', | |
'href': 'data:image/svg+xml;charset=utf-8;base64,' + base64SvgText | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment