How to convert image to base64 in javascript

The base64 algorithm is used to encode and decode data. It's often used for the encoding of binary files, but it can be used for other types of data as well. The base64 algorithm is very simple, but it does have one problem: it doesn't support Unicode characters in its encoding scheme. As a result, you can't use this algorithm to encode Unicode text.

Also check these Questions:

Javascript convert timestamp to date format
Javascript converting object to array
Convert an Int to a String in JavaScript

image to base64 js

on Jan 01, 1970
<input id="inputFileToLoad" type="file" onchange="encodeImageFileAsURL();" />
<div id="imgTest"></div>
<script type='text/javascript'>
  function encodeImageFileAsURL() {

    var filesSelected = document.getElementById("inputFileToLoad").files;
    if (filesSelected.length > 0) {
      var fileToLoad = filesSelected[0];

      var fileReader = new FileReader();

      fileReader.onload = function(fileLoadedEvent) {
        var srcData = fileLoadedEvent.target.result; // <--- data: base64

        var newImage = document.createElement('img');
        newImage.src = srcData;

        document.getElementById("imgTest").innerHTML = newImage.outerHTML;
        alert("Converted Base64 version is " + document.getElementById("imgTest").innerHTML);
        console.log("Converted Base64 version is " + document.getElementById("imgTest").innerHTML);
      }
      fileReader.readAsDataURL(fileToLoad);
    }
  }
</script>

Add Comment

1

png to base64 javascript

on Jan 01, 1970
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('GET', url);
  xhr.responseType = 'blob';
  xhr.send();
}

toDataURL('https://cdn.discordapp.com/attachments/571092147801948204/784586541146177606/6f32c864-985a-481d-8d8e-bd1f14ab9951.png', function(dataUrl) {
  console.log('RESULT:', dataUrl)
})

Add Comment

0

javascript convert image to base64

on Jan 01, 1970
function encodeImageFileAsURL(element) {
  var file = element.files[0];
  var reader = new FileReader();
  reader.onloadend = function() {
    console.log('RESULT', reader.result)
  }
  reader.readAsDataURL(file);
}
<input type="file" onchange="encodeImageFileAsURL(this)" />

Add Comment

0

To convert image to base64, use the following code which given above:

Javascript answers related to "how to convert image to base64 in javascript"

View All Javascript queries

Javascript queries related to "how to convert image to base64 in javascript"

how to convert image to base64 in javascript convert pdf to base64 javascript convert img tag to base64 javascript remove mime type from base64 javascript image image using next and previous button in javascript how to convert react component to image javascript convert string to number how to convert minutes into seconds in javascript convert milliseconds to minutes and seconds javascript javascript convert array to object how to convert an array into an object using javascript convert string to integer javascript convert date to timestamp javascript convert string of expression in to expression in javascript convert binary to string javascript javascript convert binary to text Convert seconds to hours minutes seconds javascript javascript convert string to date format yyyy-mm-dd Convert json string to json object javascript Convert int to string javascript Javascript convert timestamp to date format how to change image source using javascript area selection on image using javascript how to assign image to a variable in javascript how to add thumbnail image carousel in javascript fancybox double click sur image change javascript download image from url javascript how to convert timestamp to date in react native flutter convert json string to json how to convert string to int js convert a string to html element in js convert to jsx how to convert string into string array typescript convert color to rgb Php convert string to json convert int to string jquery angular image upload jquery save upload image how to change background image dynamically in react how to creat 6 image ui for react native how to import background image in inline css in react how to load image in react js how to make react-native circle button with image how to use saved image on react placeholder component image react react image react native app slow lagging image react native asign width to image react native background image react native bordered image drop with shadow fix upload image file in react native using rest api to website upload image in firebase storage react web view background image in react native vscode react cannot find moudle when import image ways to show image in react native vue js link image link in props doesnt work nodejs watermark image read qr code from image nodejs flutter asset image not showing flutter background image flutter decoration image crop image canvas js check if image url exists tinymce image and links inputs disabled how to add image in react js passing argument to function handler functional compoent javascript react reactjs javascript is mobile and desktop use javascript library in react node js send javascript javascript queryselectorall math.random javascript javascript onclick href location javascript get last element of array remove attribute javascript Javascript stop setInterval javascript round 2 decimals javascript to integer parse integer javascript Javascript write to text file afficher un div qui etait cache en javascript javascript in viewport javascript remove first character from string document ready javascript vanilla how to get all elements with same class in javascript application/x-www-form-urlencoded javascript fetch add 10 seconds to date javascript javascript get random floating number how to remove duplicate array object in javascript Array unique values javascript remove element from array javascript to pascal case javascript javascript multiline string window onload javascript how to check if object is empty javascript document ready javascript math floor javascript null read file javascript javascript check if string is number javascript urlencode json How to get current timestamp in javascript javascript object to json javascript startswith javascript reverse array regex space javascript javascript object entries timestamp to date javascript array length javascript javascript classlist add javascript object destructuring javascript first letter uppercase datetime to date javascript create element javascript foreach object javascript javascript redirection how to use javascript to get full file path default in javascript javascript get stack trace javascript show stack trace javascript style an element javascript object to array javascript merge objects Javascript merge two objects javascript and operator javascript replace spaces with nbsp window.open javascript auto close javascript sort array of date string.find javascript math.max in javascript javascript iterate through for loop javascript prototype explained javascript background color check online status javascript how to get nth fibonacci javascript how to print a line in javascript javascript add css file add value to array javascript for array javascript javascript template literals javascript round to 2 decimal delete element from list javascript switch statement javascript check if all elements in array are true javascript javascript send post data with ajax generate random number array javascript from principal array add css in javascript set focus on input field javascript limit characters display javascript add an element to an array javascript javascript resize event javascript format price javascript new line javascript mouse up mouse down change index array javascript javascript get array min and max javascript create array of objects with map sum all elements in array javascript get year javascript copyright draw rectangle on canvas javascript filter javascript array get data attribute javascript javascript if shorthand queryselector javascript generate random ip address javascript javascript date format mm/dd/yyyy hasownproperty javascript addeventlistener javascript multiple functions javascript if not javascript date format dd-mm-yyyy string length JavaScript javascript check undefined arrow function javascript download file javascript javascript event listener html to pdf javascript javascript download file for loop inside a for loop javascript get current date javascript yyyy-mm-dd json to array javascript javascript array find iterate over array of objects javascript find a single element in array of objects javascript while javascript javascript addeventlistener javascript loop aray javascript check undefined or null empty string in javascript interactive svg javascript make an object javascript where is select value in javascript event object javascript scrollleft stop how to stop requestanimationframe in javascript javascript add parameter to object javascript check if array is in array javascript math.pow fetch method in javascript check if checkbox is checked javascript javascript change title validate latitude longitude javascript

Browse Other Code Languages

CodeProZone