Skip to content

Instantly share code, notes, and snippets.

@stevermeister
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save stevermeister/e3f43a34fd3f5b6be04b to your computer and use it in GitHub Desktop.

Select an option

Save stevermeister/e3f43a34fd3f5b6be04b to your computer and use it in GitHub Desktop.
JavaScript File download imitation
window.downloadFile = function(sUrl) {
 
    //If in Chrome or Safari - download via virtual link click
    if (window.downloadFile.isChrome || window.downloadFile.isSafari) {
        //Creating new link node.
        var link = document.createElement('a');
        link.href = sUrl;
 
        if (link.download !== undefined){
            //Set HTML5 download attribute. This will prevent file from opening if supported.
            var fileName = sUrl.substring(sUrl.lastIndexOf('/') + 1, sUrl.length);
            link.download = fileName;
        }
 
        //Dispatching click event.
        if (document.createEvent) {
            var e = document.createEvent('MouseEvents');
            e.initEvent('click' ,true ,true);
            link.dispatchEvent(e);
            return true;
        }
    }
 
    // Force file download (whether supported by server).
    var query = '?download';
 
    window.open(sUrl + query);
}
 
window.downloadFile.isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
window.downloadFile.isSafari = navigator.userAgent.toLowerCase().indexOf('safari') > -1;
function goImage(){
html2canvas(document.getElementById('Stage_screen'), {
onrendered: function(canvas) {
downloadFile(canvas.toDataURL('image/jpeg'));
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment