Skip to content

Instantly share code, notes, and snippets.

@naveed-ahmad
Created January 14, 2018 13:57
Show Gist options
  • Save naveed-ahmad/3785cc0e6798452b0ec3828dd562af0a to your computer and use it in GitHub Desktop.
Save naveed-ahmad/3785cc0e6798452b0ec3828dd562af0a to your computer and use it in GitHub Desktop.
download= function() {
canvas=document.getElementById('canvas')
canvas.toBlob(function(blob) {
var anchor = document.createElement('a'),
dataUrl = URL.createObjectURL(blob),
fileName = 'canvas.png';
// set a attributes
anchor.setAttribute('href', dataUrl);
anchor.setAttribute('target', '_blank');
anchor.setAttribute('download', fileName);
// simulate click
if (document.createEvent) {
evtObj = document.createEvent('MouseEvents');
evtObj.initEvent('click', true, true);
anchor.dispatchEvent(evtObj);
}
else if (anchor.click) {
anchor.click();
}
});
}
@naveed-ahmad
Copy link
Author

naveed-ahmad commented Jul 12, 2019

Download Text file using

 function download(text, filename, type) {
    file = new Blob([Object.values(PAGES).join('')], {type: type})
    var a = document.createElement("a"),
          url = URL.createObjectURL(file);
        a.href = url;
        a.download = filename;
        document.body.appendChild(a);
        a.click();
        setTimeout(function() {
            document.body.removeChild(a);
            window.URL.revokeObjectURL(url);  
        }, 0); 
}


download(Object.values(PAGES).join(''), 'v2codes.txt', 'text/plain')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment