javascript - 将 SVG 字符串转换为 base64
问题描述
我希望将 SVG 字符串转换为其 base64 格式,同时尽量避免创建元素节点。
这样做的目的是使用<a>
标签创建一个可下载的链接
这是 SVG(示例):
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="1500" height="1500" viewBox="0 0 1500 1500" xml:space="preserve">
<desc>Created with Fabric.js 3.6.3</desc>
<defs>
</defs>
<g transform="matrix(1 0 0 1 25.5 300.5)" >
<rect style="stroke: rgb(0,0,0); stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,91,109); fill-rule: nonzero; opacity: 1;" x="-25" y="-150" rx="0" ry="0" width="50" height="300" />
</g>
<g transform="matrix(1 0 0 1 50.5 50.5)" >
<rect style="stroke: rgb(0,0,0); stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,91,109); fill-rule: nonzero; opacity: 1;" x="-50" y="-50" rx="0" ry="0" width="100" height="100" />
</g>
<g transform="matrix(1 0 0 1 950.5 25.5)" >
<rect style="stroke: rgb(0,0,0); stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,91,109); fill-rule: nonzero; opacity: 1;" x="-550" y="-25" rx="0" ry="0" width="1100" height="50" />
</g>
<g transform="matrix(1 0 0 1 350.5 250.5)" >
<circle style="stroke: rgb(0,0,0); stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,91,109); fill-rule: nonzero; opacity: 1;" cx="0" cy="0" r="50" />
</g>
<g transform="matrix(1 0 0 1 700.5 1050.5)" >
<rect style="stroke: rgb(0,0,0); stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,91,109); fill-rule: nonzero; opacity: 1;" x="-50" y="-50" rx="0" ry="0" width="100" height="100" />
</g>
</svg>
解决方案
你可以使用URL.createObjectURL()
var svg = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="1500" height="1500" viewBox="0 0 1500 1500" xml:space="preserve"><desc>Created with Fabric.js 3.6.3</desc><defs></defs><g transform="matrix(1 0 0 1 25.5 300.5)" ><rect style="stroke: rgb(0,0,0); stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,91,109); fill-rule: nonzero; opacity: 1;" x="-25" y="-150" rx="0" ry="0" width="50" height="300" /></g><g transform="matrix(1 0 0 1 50.5 50.5)" ><rect style="stroke: rgb(0,0,0); stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,91,109); fill-rule: nonzero; opacity: 1;" x="-50" y="-50" rx="0" ry="0" width="100" height="100" /></g><g transform="matrix(1 0 0 1 950.5 25.5)" ><rect style="stroke: rgb(0,0,0); stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,91,109); fill-rule: nonzero; opacity: 1;" x="-550" y="-25" rx="0" ry="0" width="1100" height="50" /></g><g transform="matrix(1 0 0 1 350.5 250.5)" ><circle style="stroke: rgb(0,0,0); stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,91,109); fill-rule: nonzero; opacity: 1;" cx="0" cy="0" r="50" /></g><g transform="matrix(1 0 0 1 700.5 1050.5)" ><rect style="stroke: rgb(0,0,0); stroke-width: 1; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,91,109); fill-rule: nonzero; opacity: 1;" x="-50" y="-50" rx="0" ry="0" width="100" height="100" /></g></svg>`;
// convert svg to blob url
var url = URL.createObjectURL(new Blob([svg], {type:"image/svg+xml;charset=utf-8"}));
// add the url to the a.href
document.querySelector('#download').href = url;
<a id="download" download="mysvg.svg">download svg</a>
推荐阅读
- sql - 使用 alter 命令在数据库级别永久关闭 ANSI_WARNINGS 不起作用
- java - 如何使用拖动器 2 创建一个带有模拟数据的单例类?
- python - python中的加密货币相关性,使用字典
- c# - 如何使用反射将类型对象转换为 C# 中的列表。我可以访问对象中的属性,但不能访问值,有什么建议吗?
- stenciljs - TestWindow 不是 Stenciljs 单元测试中的构造函数
- ios - 无法从 iOS 中的“MyFiles”附加和发布 pdf 文件
- highcharts - 使用轴类型“类别”时,与 x 轴相反的 Highcharts 人口金字塔未正确标记
- c# - C# - 使用 WMI 获取带有友好名称的 COM 端口
- ssl - 在 IIS 中部署应用程序
- ksqldb - 来自流主题(Json 格式)的新流,没有任何数据