Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

395
Views
How to convert svg string to svg file using Python?

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?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The solution is:

with open("svgTest.svg", "w") as svg_file:
        svg_file.write(svgImg)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!