我有一个将SVG下载为PNG的功能。它在chrome中工作得很好但是在firefox中没有触发下载。需要改变什么?
function downloadGraph(contextDivId){
var svgselect = $("#"+contextDivId).children("svg")[0],
xml = new XMLSerializer().serializeToString(svgselect);
var imgsrc = 'data:image/svg+xml;base64,'+ btoa(unescape(encodeURIComponent(xml)));
var img = '<img src="'+imgsrc+'">';
$("#svgdataurl").html(img);
var bbox = svgselect.getBBox()
var canvas = document.querySelector(".downloadcanv");
$(canvas).attr("width",bbox.width);
$(canvas).attr("height",bbox.height);
var context = canvas.getContext("2d");
var image = new Image;
image.src = imgsrc;
context.drawImage(image, 0, 0);
var canvasdata = canvas.toDataURL("image/png");
var pngimg = '<img src="'+canvasdata+'">';
$("#pngdataurl").html(pngimg);
var a = document.createElement("a");
a.download = "Graph.png";
a.href = canvasdata;
a.click();
}
答案 0 :(得分:0)
这可能会对你有所帮助。这是一个较老的帖子,但它可能会做到这一点。它为FireFox提供了不同的格式:
https://gist.github.com/jweir/706988
// Works in recent Webkit(Chrome)
$("body").append($("<img src='data:image/svg+xml;base64,\n"+b64+"' alt='file.svg'/>"));
// Works in Firefox 3.6 and Webit and possibly any browser which supports the data-uri
$("body").append($("<a href-lang='image/svg+xml' href='data:image/svg+xml;base64,\n"+b64+"' title='file.svg'>Download</a>"));