本文介绍了SVG转换成的base64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是创建编程并没有转化为正确的base64 SVG。

在我的应用我有哪些得到相克元素的响应,然后投入创建SVG元素并将其转换成一个的base64服务,但如果我尝试打开一个链接,我发现SVG不会渲染上该页面。

  VAR的xmlns ='http://www.w3.org/2000/svg',
    IMAGE_TEMPLATE =与Document.createElementNS号(xmlns,'SVG');
    IMAGE_TEMPLATE.appendChild(document.body.querySelector('#ICO家电调温-128'));
    IMAGE_TEMPLATE.setAttribute('身份证','SVG');    IMAGE_TEMPLATE.setAttributeNS(NULL,'宽',128);
    IMAGE_TEMPLATE.setAttributeNS(NULL,高度,128);
    IMAGE_TEMPLATE.setAttributeNS(NULL,视框','0 0 128 128');    document.body.querySelector('#测试)的appendChild(IMAGE_TEMPLATE)。    测试=功能(){
        变种S =新的XMLSerializer()。serializeToString(的document.getElementById(SVG))
        变种EN codedData = window.btoa(S);
        的console.log(数据:图像/ SVG + XML; BASE64,'+ EN codedData);
    }


解决方案

You didn't close test() with a }.Code below returns a base64 encoded svg:https://jsfiddle.net/seahorsepip/6sra5c5L/1/

Edit:

svg render issue is something I ran into myself before, here's the fix with a line of jquery:https://jsfiddle.net/seahorsepip/6sra5c5L/3/

//Force refresh svg
$("#test").html($("#test").html());

Here's the original SO thread about the issue: jquery's append not working with svg element?

I don't know the javascript equivalent for the jQuery code I added, I tried to write it but it didn't work :/

Edit 2:

Here's the pure js equivalent:

https://jsfiddle.net/seahorsepip/6sra5c5L/4/

//Force refresh svg
var svg = document.body.querySelector('#test').innerHTML;
document.body.querySelector('#test').innerHTML = "";
document.body.querySelector('#test').innerHTML = svg;

这篇关于SVG转换成的base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 06:54