首页 > 代码库 > js实现canvas保存图片为png格式并下载到本地

js实现canvas保存图片为png格式并下载到本地

  1. canvas 保存图片

  2. 下载到本地

    function base64Img2Blob(code){
                    var parts = code.split(‘;base64,‘);
                    var contentType = parts[0].split(‘:‘)[1];
                    var raw = window.atob(parts[1]);
                    var rawLength = raw.length;
    
                    var uInt8Array = new Uint8Array(rawLength);
    
                    for (var i = 0; i < rawLength; ++i) {
                      uInt8Array[i] = raw.charCodeAt(i);
                    }
    
                    return new Blob([uInt8Array], {type: contentType}); 
                }
                function downloadFile(fileName, content){
                   
                    var aLink = document.createElement(‘a‘);
                    var blob = base64Img2Blob(content); //new Blob([content]);
                  
                    var evt = document.createEvent("HTMLEvents");
                    evt.initEvent("click", false, false);//initEvent 不加后两个参数在FF下会报错
                    aLink.download = fileName;
                    aLink.href = http://www.mamicode.com/URL.createObjectURL(blob);"image/png"));
    

      

js实现canvas保存图片为png格式并下载到本地