D3图表图像下载无法在Firefox中运行

时间:2017-08-08 11:36:27

标签: javascript jquery firefox download

我已经编写了这段代码,这些代码在Google Chrome和Opera上完美运行但不适用于Firefox:

[ODBC][61608][1502282408.929695][SQLSetConnectAttr.c][396]
        Entry:
            Connection = 0x7f71840008c0
            Attribute = SQL_ATTR_AUTOCOMMIT
            Value = (nil)
            StrLen = -5

有什么问题?请指导我。

1 个答案:

答案 0 :(得分:1)

在我的案例中工作的是

         d3.select("#savebutton").on("click", function(){  
          var html = d3.select("svg") 
          .attr("version", 1.1)
          .attr("xmlns", "http://www.w3.org/2000/svg")
          .node( ).parentNode.innerHTML;    

    //console.log(html);

var imgsrc = 'data:image/svg+xml;base64,'+ btoa(html);
var image = new Image;
image.src = imgsrc;

image.onload = function() {
var canvas = document.createElement('canvas');
canvas.width = image.width;
canvas.height = image.height;
var context = canvas.getContext('2d');
context.fillStyle = "#FFFFFF";
context.fillRect(0,0,image.width,image.height);
context.drawImage(image, 0, 0);

var a = document.createElement('a');
a.download = "sampleidvgraph.png";
a.href = canvas.toDataURL('image/png');
document.body.appendChild(a);
a.click().attr('target', '_blank');
}
});