function GetImgToBase64 (url,callback) {//Convert picture to Base64 var canvas = document.createelement (' canvas '), CTX = Canvas.getcontext (' 2d '), img = new Image; Img.crossorigin = ' Anonymous '; Img.onload = function () { canvas.height = img.height; Canvas.width = Img.width; Ctx.drawimage (img,0,0); var dataurl = Canvas.todataurl (' image/png '); Callback (Dataurl); canvas = null; }; img.src = URL;}
function Dataurltofile (dataurl, filename) {//convert Base64 to File
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});
}
You can convert a picture to Base64
GetImgToBase64 (' Img/test.png ', function (data) {
var myFile = dataurltofile (data, ' testimgtestimgtestimg ');
Console.log (MyFile);
});
JS Picture Conversion Base64 Base64 convert to file object