How to save svg canvas to local filesystem

前端 未结 16 1495
南旧
南旧 2020-11-27 10:10

Is there a way to allow a user, after he has created a vector graph on a javascript svg canvas using a browser, to download this file to their local filesystem?

SVG

16条回答
  •  误落风尘
    2020-11-27 10:14

    I'm replying to this topic, even though it's a few years old, because the recent convergence of web browsers in their support for SVG and other relevant behaviour has produced renewed interest in SVG and allows a 'universal' answer to the question. In essence zneak's approach is correct but, in my opinion, terse (i.e. it took me a while to get it working for myself). I also think that his reference to AJAX is either unnecessary or not what I understand by AJAX (= uses an XMLHttpRequest). I shall therefore provide a more detailed answer using pure JavaScript (i.e. without JQuery or any other library) and provide server code for Java, Perl and PHP.

    (1) Have the (dynamically generated) SVG content in your HTML page enclosed in a div with a unique ID, e.g.

    SVG content

    (2) Include a button to invoke the JavaScript function, e.g.

    (3) Include the JavaScript function named in your button markup:

    function sendSVG() 
    {
       var svgText = document.getElementById('svg').innerHTML;
    
       var form = document.createElement("form");
       form.setAttribute("method", "post");
       form.setAttribute("action", "http://path-to-your-server-app");
       form.setAttribute("accept-charset", "UTF-8");
    
       var hiddenSVGField = document.createElement("input");    
       hiddenSVGField.setAttribute("type", "hidden");
       hiddenSVGField.setAttribute("name", "svgText");
       hiddenSVGField.setAttribute("value", svgText);
    
       form.appendChild(hiddenSVGField);
       document.body.appendChild(form);
       form.submit();
    }
    

    (4) Write a server app to accept your SVGtext post request and return as image/svg+xml using Content-Disposition to specify an attachment. Working code in three languages is presented, although I am not a Perl programmer and have never used PHP in anger.

    Java Servlet

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
       String svgText = (String) request.getParameter("svgText");
       response.setContentType("image/svg+xml");
       response.addHeader("Content-Disposition", "attachment; filename=\"image.svg\"");
       PrintWriter out = response.getWriter();
       out.println(svgText);
    }
    

    Perl CGI

    use CGI qw(:standard);
    my $svgText = param('svgText');
    print header(-type => "image/svg+xml",
        -content_disposition => "attachment; filename=image.svg");      
    print $svgText;
    

    PHP

    
    

    I have used a hard-coded name for the image here (image.svg), but actually pick up a discriptor of the dynamic content I generate from the page (using a div and an ID again, and document.getElementById('graphName').textContent).

    This has been tested on Mac Safari 9, Firefox 42, Chrome 47, Opera 34, and Windows7/IE 11 and Windows10/Edge and in each case the svg file is downloaded or one is prompted to download it. The resulting files will open in, e.g. Adobe Illustrator or whatever other application you have set to open svg files.

    A real-world example of this (if you consider academic research real-world) is at http://flyatlas.gla.ac.uk/MidgutAtlas/index.html in the Gene section.

提交回复
热议问题