base64转blob、base64转文件流

//base64转文件流
export function base64ToFile (base64, fileName) {
    let bstr = atob(base64), n = bstr.length, u8arr = new Uint8Array(n);
    while(n--){
        u8arr[n] = bstr.charCodeAt(n);
    }
    return new File([u8arr], fileName, {type: "image/jpg"});
};

//base64转blob
export function base64ToBlob(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 });
}
//获取base64转blob之后的缓存路径
export function downloadFileByBase64(base64){
    var myBlob = base64ToBlob(base64)
    var myUrl = URL.createObjectURL(myBlob)
    return myUrl;
}

你可能感兴趣的:(base64转blob、base64转文件流)