简体   繁体   中英

Javascript: How to Save Image with a particular filename instead of download.png

I am trying to download the file as helloWorld.png but it always saves as download.png.

Can you help me identify what i am missing?

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/

try this jquery code, it works on the latest Firefox, (where uri is the datauri and name is the image filename)

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


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM