图片 base64 file blob 之间相互的转化

//base64码转化为文件
// dataURLtoFile(dataurl, filename) {
// let arr = dataurl.split(',');
// let mime = arr[0].match(/:(.*?);/)[1];
// let bstr = atob(arr[1]);
// let n = bstr.length;
// let u8arr = new Uint8Array(n);
// while(n--) {
// u8arr[n] = bstr.charCodeAt(n)
// }
// return new File([u8arr], filename, {type:mime})
// },
// getBase64(fileObj) {
// let file = fileObj;
// let cvs = document.createElement("canvas");
// let ctx = cvs.getContext("2d");
// if(file) {
// var url = window.URL.createObjectURL(file);//PS:不兼容IE
// var img = new Image();
// img.src = url;
// img.onload = function() {
// ctx.clearRect(0, 0, cvs.width, cvs.height);
// cvs.width = img.width;
// cvs.height = img.height;
// ctx.drawImage(img, 0, 0, img.width, img.height);
// var base64 = cvs.toDataURL("image/png");
// //callback(base64);
// }
// }
// },
blobToFile(theBlob, fileName) {
theBlob.lastModifiedDate = new Date();
theBlob.name = fileName;
return theBlob;
},
dataURLtoBlob(dataurl) {
var arr = dataurl.split(',');
let mime = arr[0].match(/:(.*?);/)[1];
let bstr = atob(arr[1]);
let n = bstr.length;
let u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], { type: mime });
}

猜你喜欢

转载自www.cnblogs.com/lhqdbk/p/12626568.html