On the client side I have an svg element, under which there's an image and some text on that image (so it looks like a poster with text on it). I need to send this poster as a whole to the server. The HTML looks like this:
<svg id="poster_svg" width="1000" height="1000" style="max-width: 100%;">
<image href="/static/images/motiv1.jpg" width="100%" height="100%"/>
<p class="centered" id="poster_text"></p>
</svg>
The text inside the p
is generated by some other user actions. I found a tutorial online which converts the svg element to base64 which then I can use to send to server through a POST request.
The JavaScript code for it is:
var SVGDomElement = document.getElementById("poster_svg");
var serializedSVG = new XMLSerializer().serializeToString(SVGDomElement);
var base64Data = window.btoa(serializedSVG);
const json = {
image: base64Data
};
const options = {
method: "POST",
body: JSON.stringify(json),
headers:{
'Content-Type':'application/json'
}
};
fetch('/makeposter',options).then(res=>{
return res.json()
}).then(cont=>{
console.log(cont)
});
On the server side (which uses Flask), the base64 is recieved successfully, but when converted to an image, it is blank. The server code for converting the base64 to image is this:
cont = request.get_json()
imgb64 = cont['image']
decoded = base64.b64decode(imgb64)
f=open("test.png","wb")
f.write(decoded)
f.close()
Why is the image blank, and how can I fix it?