Guest User

Load binary file in JavaScript and convert to base64

a guest
Jul 19th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var url = 'file:///c/temp/file.xlsx';
  2. var xhr = new XMLHttpRequest();
  3. xhr.open("get", url, true);
  4. xhr.onreadystatechange = function () {
  5.     if (xhr.readyState == 4 && xhr.status == 200) {
  6.         var blob = new Blob([xhr.response], {
  7.                 type: "octet/stream"
  8.             });
  9.         var reader = new window.FileReader();
  10.         reader.readAsDataURL(blob);
  11.         reader.onloadend = function () {
  12.             base64data = reader.result;
  13.             console.log(base64data);
  14.         }
  15.     }
  16. }
  17. xhr.responseType = "arraybuffer";
  18. xhr.send();
Add Comment
Please, Sign In to add comment