Javascript Base64转Blob

转换方法

function 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 theBlob = new Blob([u8arr], {type: mime });
    theBlob.lastModifiedDate =new Date();
    theBlob.name = filename;
    return theBlob;
}

图片上传

var images = new FormData();
images.append('images[]', dataURLtoFile(base64, filename), filename);
$.ajax({
    url: 'uploadImage',
    async: false,
    type: 'post',
    data: images,
    processData: false,
    contentType: false,
    success: function(data){
    //TODO
    }
});

 

posted @ 2021-04-19 10:11  蝶花残梦  阅读(1381)  评论(0编辑  收藏  举报
Live2D