Javascript:如何使用特定文件名而不是download.png保存图像

时间:2014-06-04 22:03:00

标签: javascript jquery canvas canvg

我正在尝试将该文件下载为helloWorld.png,但它始终保存为download.png。

你能帮我辨别一下我错过的东西吗?

var $container = $('#svg-container'),
        // Canvg requires trimmed content
    content = $container.html().trim(),
    canvas = document.getElementById('thecanvas');
    // Draw svg on canvas
    canvg(canvas, content);
    // Change img be SVG representation
    var theImage =  canvas.toDataURL('image/png');
    $('#svg-img').attr('src', theImage);



function downloadWithName(uri, name) {

    function eventFire(el, etype){
        if (el.fireEvent) {
            (el.fireEvent('on' + etype));
        } else {
            var evObj = document.createEvent('Events');
            evObj.initEvent(etype, true, false);
            el.dispatchEvent(evObj);
        }
    }

    var link = document.createElement("a");
    link.download = name;
    link.href = uri;
    eventFire(link, "click");

}

downloadWithName(theImage, "helloWorld.png")

http://jsfiddle.net/shanthisivanesan/a2FLx/22/

1 个答案:

答案 0 :(得分:0)

试试这个jquery代码,它适用于最新的Firefox,(其中 uri 是数据库,名称是图像文件名)

$('<a>',{
    type: "submit",
    download: name,
    href: uri,
    id: 'download_link'
}).appendTo('body');


$('a#download_link')[0].click();