js base64转blob

754 阅读1分钟

由于安卓手机浏览器不支持new File,所以要将base64的图片转成blob传给后端,方法如下

    theBlob.lastModifiedDate = new Date();
    theBlob.name = fileName;
    return theBlob;
},
dataURLtoFile(dataurl, filename){
    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);
    }
    var blob = this.dataURLtoBlob(dataurl);
    return this.blobToFile(blob, filename);
}```