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 });
}
file 转 base64
* 获取文件的Base64
function fileToBase64(file){ //file转bse64
let reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function (e) {
return e.target.result
}
}
/**
* file转base64
*/
const blobToDataURL = (blob, cb) => {
let reader = new FileReader();
reader.onload = function (evt) {
let base64 = evt.target.result;
cb(base64);
};
reader.readAsDataURL(blob);
};
export default blobToDataURL;import blobToDataURL from '../js/blobToDataURL';
//....
// 选择图片
inputImage (e) {
let img = e.target.files[0];
if(img) {
let url = URL.createObjectURL(img);
let base64 = blobToDataURL(img, (base64Url) => {
this.inputImg = base64Url;
});
}
},