Convert Image To Base64 String Using Jquery

Using an Image File, I am getting the url of an image, that needs be to skết thúc to lớn a webservice. From there the image has lớn be saved locally on my system.

The code I am using:

var imagepath = $("https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpg#imageid"https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpg).val();// from this getting the path of the selected imagethat var st = imagepath.replace(data:image/png or jpg; base64"https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpg/"https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpg"https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpg);How khổng lồ convert the image url to BASE64?


Bạn đang xem: Convert image to base64 string using jquery

*

*

HTML

*

HaNdTriX

Basically, he implemented this function:

function toDataUrl(url, callback) var xhr = new XMLHttpRequest(); xhr.onload = function() var reader = new FileReader(); reader.onloadend = function() callback(reader.result); reader.readAsDataURL(xhr.response); ; xhr.open("https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpgGET"https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpg, url); xhr.responseType = "https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpgblob"https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpg; xhr.send();And in your case, you can use it like this:

toDataUrl(imagepath, function(myBase64) console.log(myBase64); // myBase64 is the base64 string);

*

Xem thêm: Ozdic Là Gì - Toefl Cô Giang

*

This is your html-

*
Javascript should be-

var can = document.getElementById("https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpgimgCanvas"https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpg); var img = document.getElementById("https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpgimageid"https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpg); var ctx = can.getContext("https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpg2d"https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpg); ctx.drawImage(img, 10, 10); var encodedBase = can.toDataURL();"https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpgencodedBase"https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpg Contains Base64 Encoding of Image.


const getBase64FromUrl = async (url) => const data = await fetch(url); const blob = await data.blob(); return new Promise((resolve) => const reader = new FileReader(); reader.readAsDataURL(blob); reader.onloadend = function() const base64data = reader.result; resolve(base64data); );getBase64FromUrl("https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpghttps://lh3.googleusernội dung.com/i7cTyGnCwLIJhT1t2YpLW-zHt8ZKalgQiqfrYnZQl975-ygD_0mOXaYZMzekfKW_ydHRutDbNzeqpWoLkFR4Yx2Z2bgNj2XskKJrfw8"https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpg).then(console.log)
You Can Used This :

function ViewImage() function getBase64(file) return new Promise((resolve, reject) => const reader = new FileReader(); reader.readAsDataURL(file); reader.onload = () => resolve(reader.result); reader.onerror = error => reject(error); );var file = document.querySelector("https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpginput"https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpg).files<0>;getBase64(file).then(data =>$("https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpg#ImageBase46"https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpg).val(data));Add To Your Input onchange=ViewImage();


Here"https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpgs the Typescript version of Abubakar Ahmad"https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpgs answer

function imageTo64( url: string, callback: (path64: string | ArrayBuffer) => void): void const xhr = new XMLHttpRequest(); xhr.open("https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpgGET"https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpg, url); xhr.responseType = "https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpgblob"https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpg; xhr.send(); xhr.onload = (): void => const reader = new FileReader(); reader.readAsDataURL(xhr.response); reader.onloadend = (): void => callback(reader.result);
I try using the top answer, but it occur Uncaught DOMException: Failed to lớn exedễ thương "https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpgtoDataURL"https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpg on "https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpgHTMLCanvasElement"https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpg: Tainted canvases may not be exported.

I found this is because of cross domain name problems, the solution is

function convert(oldImag, callback) var img = new Image(); img.onload = function() callback(img) img.setAttribute("https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpgcrossorigin"https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpg, "https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpganonymous"https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpg); img.src = oldImag.src;function getBase64Image(img,callback) convert(img, function(newImg) var canvas = document.createElement("https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpgcanvas"https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpg); canvas.width = newImg.width; canvas.height = newImg.height; var ctx = canvas.getContext("https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpg2d"https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpg); ctx.drawImage(newImg, 0, 0); var base64=canvas.toDataURL("https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpgimage/png"https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpg); callback(base64) )getBase64Image(document.getElementById("https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpgimageid"https://obatambeienwasirherbal.com/convert-image-to-base64-jquery/imager_6_21997_700.jpg),function(base64)// base64 in here. console.log(base64));