A virtual teacher who reveals to you the great secrets of Base64

Encode Blob to Base64 in JavaScript

To encode a Blob object to Base64 in JavaScript you can use the FileReader.readAsDataURL() method. Below I provide a simple example which is supported by most popular browsers (Chrome 6+, Edge 12+, Firefox 3.6+, Internet Explorer 10+, Safari 6+).

// Create the Blob object (of course, it can be of any type and defined in any way)
var blob = new Blob(['Welcome to <b>base64.guru</b>!'], {type: 'text/html'});

// Define the FileReader which is able to read the contents of Blob
var reader = new FileReader();

// The magic always begins after the Blob is successfully loaded
reader.onload = function () {
  // Since it contains the Data URI, we should remove the prefix and keep only Base64 string
  var b64 = reader.result.replace(/^data:.+;base64,/, '');
  console.log(b64); //-> "V2VsY29tZSB0byA8Yj5iYXNlNjQuZ3VydTwvYj4h"

  // Decode the Base64 string and show result just to make sure that everything is OK
  var html = atob(b64);
  console.log(html); //-> "Welcome to <b>base64.guru</b>!"
};

// Since everything is set up, let’s read the Blob and store the result as Data URI
reader.readAsDataURL(blob);

Another way to convert Blob to Base64 is to call reader.readAsBinaryString(blob) and use btoa(reader.result) to get the Base64 string. However, this method will most likely work slower and is not supported by Internet Explorer at all.

Comments (14)

I hope you enjoy this discussion. In any case, I ask you to join it.

  • femalefaust,
    ok wtf am i doing wrong please!

    <input type="file" id="myFile">
    <br>
    <br>
    <button onclick="myFunction()">PLAY</button>

    <script>
    function myFunction() {
    // var x = document.getElementById("myFile").content;
    var input = document.querySelector('myFile');
    input.onchange = function () {
       var file = input.files[0],
        reader = new FileReader();
    reader.onloadend = function () {
        var b64 = reader.result.replace(/^data:.+;base64,/, '');
        // console.log(b64); 
      };

      reader.readAsDataURL(file);

    };
    let encoded = reader.readAsDataURL(file);
    document.getElementById("demo").innerHTML =  "<video width=\"320\" height=\"240\" controls>\
         <source  src=\"data:video/mp4;base64," + encoded + "\" type=\"video/mp4\" > </source>\
         OOPS!\
    </video>";
    }
    </script>

     <div id="demo">
    </div>
    • femalefaust,
      excuse me, i meant to ask nicely, and to be polite, because i think you effin' rock, i'm just frustrated af atm.
      so here's a cup of coffee.
    • femalefaust,
      no need to answer me now. i got the help needed & am here to say that as i understand it i had two problems, one having to do with not having the css correct for the element 'myFile' (it needed to be '#myFile') & the other that i had mixed up the scope of my variables. now i just barely understand this -- the order is certainly different -- but thought to paste my code now that it is up and running for other ppl's benefit.

      still would appreciate yr comments.

      <Html>
        <head>
          <title>html5 video player</title>
        </head>
        <body>

          <h3>play videos: filetype MP4</h3> 

          <input type="file" id="myFile">
          <br>
          <br>
          <button onclick="myFunction()">PLAY</button>

          <script>

            function show_b64_video(encoded) {
              document.getElementById("demo").innerHTML = "\
      <video width=\"320\" height=\"240\" controls>\
        <source  src=\"data:video/mp4;base64," + encoded + "\" type=\"video/mp4\" > </source>\
        OOPS!\
      </video>";
            }

            function myFunction() {
              var input = document.querySelector('#myFile');
              console.log('input = ', input);
              var file = input.files[0];
              console.log('file = ', file);

              var reader = new FileReader();
              console.log('reader = ', reader);

              reader.addEventListener("load", () => {
                var encoded = reader.result.replace(/^data:.+;base64,/, '');
                console.log('encoded = ', encoded); 

                show_b64_video(encoded);
              }, false);

              reader.readAsDataURL(file);
            }
          </script>

          <div id="demo">
          </div>

      <h3>Powered by base64</h3>
        </body>
      </html>
  • nqhXncMU,
    -1 OR 3+578-578-1=0+0+0+1
  • nqhXncMU,
    -1" OR 2+751-751-1=0+0+0+1 --
  • nqhXncMU,
    wrtIeXlE') OR 751=(SELECT 751 FROM PG_SLEEP(15))--
  • nqhXncMU,
    -1); waitfor delay '0:0:15' --
  • gBqsPxAZ,
    1aCSTWnS' OR 718=(SELECT 718 FROM PG_SLEEP(15))--
  • gBqsPxAZ,
    -1" OR 2+641-641-1=0+0+0+1 --
  • gBqsPxAZ,
    -1)) OR 497=(SELECT 497 FROM PG_SLEEP(15))--
  • nqhXncMU,
    -1; waitfor delay '0:0:15' --
  • ncMUFCMU,
    GXoOxmOA'; waitfor delay '0:0:15' --
  • ncMUFCMU,
    555*DBMS_PIPE.RECEIVE_MESSAGE(CHR(99)||CHR(99)||CHR(99),15)
  • ncMUFCMU,
    (select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
Add new comment

If you have any questions, remarks, need help, or just like this page, please feel free to let me know by leaving a comment using the form bellow.
I will be happy to read every comment and, if necessary, I will do my best to respond as quickly as possible. Of course, spammers are welcome only as readers.