欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript如何將base64圖片轉(zhuǎn)化為URL格式

 更新時間:2023年07月24日 10:38:21   作者:這是個問題  
這篇文章主要給大家介紹了關(guān)于JavaScript如何將base64圖片轉(zhuǎn)化為URL格式的相關(guān)資料,Base64是一種編碼方式,而不是真正的加密方式,即使算Base64也僅用作一個簡單的加密來保護(hù)某些數(shù)據(jù),而真正的加密通常都比較繁瑣,需要的朋友可以參考下

1)將base64圖片格式轉(zhuǎn)為可讀的url格式

將圖片文件轉(zhuǎn)為二進(jìn)制,然后通過URL的createObjectURL函數(shù),將二進(jìn)制轉(zhuǎn)為url格式

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

2)將圖片轉(zhuǎn)為文件

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
        })
    }

總結(jié)

到此這篇關(guān)于JavaScript如何將base64圖片轉(zhuǎn)化為URL格式的文章就介紹到這了,更多相關(guān)base64圖片轉(zhuǎn)化URL格式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論