美文网首页
base64转Blob,下载blob、canvas对象

base64转Blob,下载blob、canvas对象

作者: adtk | 来源:发表于2019-08-07 16:49 被阅读0次

    base64转Blob

        function base64ToBlob(content) {
                let parts = content.split(';base64,');
                let contentType = parts[0].split(':')[1];
                let raw = window.atob(parts[1]);
                let rawLength = raw.length;
    
                let uInt8Array = new Uint8Array(rawLength);
    
                for (let i = 0; i < rawLength; ++i) {
                    uInt8Array[i] = raw.charCodeAt(i);
                }
                return new Blob([uInt8Array], { type: contentType });
            },
    

    下载blob对象

    
    let aLink = document.createElement('a');
    aLink.download = "fileName";
    
    let blob = base64ToBlob(content);
    aLink.href = URL.createObjectURL(blob);
    
    aLink.click();
    

    下载canvas图片

    
                var save_url = canvas.toDataURL("image/png");
                var a = document.createElement('a');
                a.href = save_url;
                a.download = "codeName";
                a.click();
    

    相关文章

      网友评论

          本文标题:base64转Blob,下载blob、canvas对象

          本文链接:https://www.haomeiwen.com/subject/achadctx.html