简体   繁体   English

将 SVG 标签内的图像发送到服务器 - JavaScript

[英]Send image inside SVG tag to server - JavaScript

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).在客户端,我有一个 svg 元素,在该元素下有一个图像和该图像上的一些文字(所以它看起来像一张带有文字的海报)。 I need to send this poster as a whole to the server.我需要将这张海报作为一个整体发送到服务器。 The HTML looks like this: HTML 看起来像这样:

    <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. p中的文本是由其他一些用户操作生成的。 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.我在网上找到了一个教程,它将 svg 元素转换为 base64,然后我可以使用它通过 POST 请求发送到服务器。

The JavaScript code for it is:它的 JavaScript 代码是:

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.在服务器端(使用 Flask),base64 被成功接收,但是当转换为图像时,它是空白的。 The server code for converting the base64 to image is this:将 base64 转换为图像的服务器代码是这样的:

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?为什么图像是空白的,我该如何解决?

base64-encoding does not magically convert SVG to PNG. base64 编码不会神奇地将 SVG 转换为 PNG。 After decoding on the server, the result will still be a string, and its content will be still the same serialized SVG you quoted for the client side.在服务器上解码后,结果仍然是一个字符串,其内容仍然是您为客户端引用的相同的序列化 SVG。

This includes the fact that the image tag still only holds a reference to the JPG file, not the file content itself.这包括图像标签仍然只包含对 JPG 文件的引用,而不是文件内容本身的事实。 You will need to make sure that the relative path to that file is still valid in the context of server-side execution.您需要确保该文件的相对路径在服务器端执行的上下文中仍然有效。

Lastely, if you want to convert the result from SVG to PNG, you need some software to do the actual work.最后,如果要将结果从 SVG 转换为 PNG,则需要一些软件来完成实际工作。 I have no further knowledge how you would go about that with Flask.我不知道您将如何使用 Flask 来了解 go。

You could also do the conversion client-side, see for example this question for a how-to.您也可以在客户端进行转换,例如,请参阅此问题以获取操作方法。 Just exchange the download function described there with an upload function.只需将下载的 function 与上传的 function 交换即可。 But note that there is a serious caveat: the <image> href inside the SVG must point to the same domain as the page this is executed on, otherwise export will be blocked .但请注意,有一个严重的警告:SVG 中的<image> href必须指向与执行此操作的页面相同的域,否则导出将被阻止

Aside Using a HTML <p> tag directly inside SVG content does not work.除了在 SVG 内容中直接使用 HTML <p>标记之外,它不起作用。 You have to use a <text> element.您必须使用<text>元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM