Using an Image File, I am getting the url of an image, that needs be to send to a webservice. From there the image has to be saved locally on my system.

The code I am using:

var imagepath = $(“”;// from this getting the path of the selected imagethat var st = imagepath.replace(data:image/png or jpg; base64″””;How to convert the image url to BASE64?

Đang xem: Convert image to base64 string using jquery






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); };“”, url); xhr.responseType = “”; 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:


This is your html-


Javascript should be-

var can = document.getElementById(“”; var img = document.getElementById(“”; var ctx = can.getContext(“”; ctx.drawImage(img, 10, 10); var encodedBase = can.toDataURL();”” 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(“″
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(““<0>;getBase64(file).then(data =>$(“″;}Add To Your Input onchange=ViewImage();

Here” the Typescript version of Abubakar Ahmad” answer

function imageTo64( url: string, callback: (path64: string | ArrayBuffer) => void): void { const xhr = new XMLHttpRequest();“”, url); xhr.responseType = “”; xhr.send(); xhr.onload = (): void => { const reader = new FileReader(); reader.readAsDataURL(xhr.response); reader.onloadend = (): void => callback(reader.result); }}

Xem thêm: Rtmp Là Gì ? Định Nghĩa Và Giải Thích Ý Nghĩa Tìm Hiểu Về Công Nghệ Livestream

I try using the top answer, but it occur Uncaught DOMException: Failed to execute “” on “” Tainted canvases may not be exported.

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

function convert(oldImag, callback) { var img = new Image(); img.onload = function(){ callback(img) } img.setAttribute(“”, “”; img.src = oldImag.src;}function getBase64Image(img,callback) { convert(img, function(newImg){ var canvas = document.createElement(“”; canvas.width = newImg.width; canvas.height = newImg.height; var ctx = canvas.getContext(“”; ctx.drawImage(newImg, 0, 0); var base64=canvas.toDataURL(“”; callback(base64) })}getBase64Image(document.getElementById(“”,function(base64){// base64 in here. console.log(base64)});

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *