How to give browser “save image as” option to button

做~自己de王妃 提交于 2019-11-30 02:31:20

In modern browser you can use the download attribute

function save2() {
    window.open(canvas.toDataURL('image/png'));
    var gh = canvas.toDataURL('png');

    var a  = document.createElement('a');
    a.href = gh;
    a.download = 'image.png';

    a.click()
}

just trigger the function from a button, or insert the anchor in the page and style it as a button.

FIDDLE

Ashwin2000

First create a button for it
<a href="#" class="button" id="btn-download" download="my-file-name.png">Download</a>

Then add the following in javascript

var button = document.getElementById('btn-download');
button.addEventListener('click', function (e) {
    var dataURL = canvas.toDataURL('image/png');
    button.href = dataURL;
});

I have made an example for you Check this out!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!