1

Using AJAX I send a svg image to Django using the following function:

function uploadSVG(){

    var svgImage = document.getElementById("SVG");

    var serializer = new XMLSerializer();
    var svgStr = serializer.serializeToString(svgImage);

    $(document).ready(function(){
        $.post("ajax_upload_svg/",
      {
        csrfmiddlewaretoken: csrftoken,
        svgImage: svgStr
      },
      function(){
        console.log('Done')
      });
    });
}

In Django I end up with the svg image as a string using the following function:

def uploadSVG(request):

    svgImg = request.POST.get('svgImage')

    return HttpResponse('')

The string I get looks like this:

<svg xmlns="http://www.w3.org/2000/svg" id="SVG" width="460" height="300" style="border:2px solid #000000"><rect x="150" y="70" width="160" height="130" fill="#292b2c"/></svg>

How can I convert this svg string into a svg file?

1
  • 4
    Just save it as an SVG file would be my guess. Commented Feb 13, 2022 at 20:06

1 Answer 1

2

The solution is:

with open("svgTest.svg", "w") as svg_file:
        svg_file.write(svgImg)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.