如何将base64图片转化为URL格式

1)将base64图片格式转为可读的url格式
将图片文件转为二进制,然后通过URL的createObjectURL函数,将二进制转为url格式

   function getBase64URL(pic) {
    
    
        const blob = base64ImgtoFile(pic)
        const blobUrl = window.URL.createObjectURL(blob);
        return blobUrl
    }

2)将图片转为文件

function base64ImgtoFile (dataurl, filename = 'file') {
    
    
        //将base64格式分割:['data:image/png;base64','XXXX']
        const arr = dataurl.split(',')
        // .*? 表示匹配任意字符到下一个符合条件的字符 刚好匹配到:
        // image/png
        const mime = arr[0].match(/:(.*?);/)[1]  //image/png
        //[image,png] 获取图片类型后缀
        const suffix = mime.split('/')[1] //png
        const bstr = atob(arr[1])   //atob() 方法用于解码使用 base-64 编码的字符串
        let n = bstr.length
        const u8arr = new Uint8Array(n)
        while (n--) {
    
    
            u8arr[n] = bstr.charCodeAt(n)
        }
        return new File([u8arr], `${
      
      filename}.${
      
      suffix}`, {
    
    
            type: mime
        })
    }

猜你喜欢

转载自blog.csdn.net/weixin_39423672/article/details/127975945