妙博客

主机评测 香港服务器 洛杉矶VPS测评

js/html下载base64编码的图片

function downBase64(base64, fileName){
        function dataURLtoBlob(dataurl) {
            var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
                bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
            while (n--) {
                u8arr[n] = bstr.charCodeAt(n);
            }
            return new Blob([u8arr], { type: mime });
        }
        function downloadFile(url,name){
            var a = document.createElement("a")
            a.setAttribute("href",url)
            a.setAttribute("download",name)
            a.setAttribute("target","_blank")
            let clickEvent = document.createEvent("MouseEvents");
            clickEvent.initEvent("click", true, true);
            a.dispatchEvent(clickEvent);
        }
        function downloadFileByBase64(base64,name){
            var myBlob = dataURLtoBlob(base64)
            var myUrl = URL.createObjectURL(myBlob)
            downloadFile(myUrl,name)
        }
        downloadFileByBase64(base64, fileName);
    }


Copyright Your 142132.com Rights Reserved. 赣ICP备17010829号-2