Base64,Blob,File 之间互转

Base64转Blob

base64ToBlob(base64Data) {
    let arr = base64Data.split(','),
        fileType = arr[0].match(/:(.*?);/)[1],
        bstr = atob(arr[1]),
        l = bstr.length,
        u8Arr = new Uint8Array(l)

    while (l--) {
        u8Arr[l] = bstr.charCodeAt(l)
    }
    return new Blob([u8Arr], {
        type: fileType,
    })
}

Blob转File

blobToFile(blob, fileName) {
    return new File([blob], `${fileName}.png`)
},

Blob转临时路径

blobToUrl(blob) {
    return window.URL.createObjectURL(blob)
},

Echarts图导出Base64

// 基于准备好的dom,初始化echarts实例
let myChart = echarts.init(document.getElementById('tmb_app'))
// 绘制图表
myChart.setOption(option)
// 获取图片地址 - base64
let baseImage = myChart.getDataURL()
posted @ 2022-02-16 15:56  Y-X南川  阅读(210)  评论(0编辑  收藏  举报