1 function dataURLtoFile(dataurl, filename) {
2
3 var arr = dataurl.split(','),
4 mime = arr[0].match(/:(.*?);/)[1],
5 bstr = atob(arr[1]),
6 n = bstr.length,
7 u8arr = new Uint8Array(n);
8
9 while(n--){
10 u8arr[n] = bstr.charCodeAt(n);
11 }
12
13 return new File([u8arr], filename, {type:mime});
14 }
15
16 //Usage example:
17 var file = dataURLtoFile('data:text/plain;base64,aGVsbG8gd29ybGQ=','hello.txt');
18 console.log(file);
1function getBase64Image(src, callback, outputFormat) {
2 const img = new Image();
3 img.crossOrigin = 'Anonymous';
4 img.onload = () => {
5 const canvas = document.createElement('canvas');
6 const ctx = canvas.getContext('2d');
7 let dataURL;
8 canvas.height = img.naturalHeight;
9 canvas.width = img.naturalWidth;
10 ctx.drawImage(img, 0, 0);
11 dataURL = canvas.toDataURL(outputFormat);
12 callback(dataURL);
13 };
14
15 img.src = src;
16 if (img.complete || img.complete === undefined) {
17 img.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
18 img.src = src;
19 }
20 }