base64转Blob,下载blob、canvas对象

2019-08-07  本文已影响0人  adtk

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();
上一篇下一篇

猜你喜欢

热点阅读