8***@qq.com
8***@qq.com
  • 发布:2024-03-03 23:26
  • 更新:2024-03-05 19:41
  • 阅读:425

h5保存base64图片/canvas图片

分类:HBuilderX

h5保存base64图片/canvas图片

/**  
         * h5保存base64图片  
         * @param {string} base64Image base64图片  
         * @param {string} fileName 文件名称,如 1.png  
         */  
        saveBase64Image(base64Image,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(base64Image,fileName);  
        }
2024-03-03 23:26 负责人:无 分享
已邀请:
HRK_01

HRK_01

请问有什么问题吗?你是提bug还是分享文章?

要回复问题请先登录注册