1、直接将base64转file对象(有兼容问题)(ie edge 360兼容模式)
//将base64转换成file对象
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);
}
return new File([u8arr], filename, {type:mime});
}
dataurl:base64
filename:文件名
2、将base转换成blob对象,再将blob对象转换成file对象。
//将base64转换为blob
function dataURLtoBlob(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 });
}
//将blob转换成file
function blobToFile(theBlob, fileName){
theBlob.lastModifiedDate =new Date();
theBlob.name = fileName;
return theBlob;
}
blobToFile(dataURLtoBlob(dataurl), fileName);
dataurl:base64
filename:文件名