mingliangguo
8/1/2016 - 3:38 AM

box get preview data URI

box get preview data URI

var xhr = new XMLHttpRequest();
xhr.open("GET", "https://api.box.com/2.0/files/76537235577/thumbnail.png?min_height=150&min_width=150", true);
xhr.setRequestHeader("Authorization", "Bearer your_access_token");
xhr.responseType = "arraybuffer";

xhr.onload = function(e) {
  if (this.status == 200) {
    var uInt8Array = new Uint8Array(this.response);
    var i = uInt8Array.length;
    var binaryString = new Array(i);
    // while (i--)
    // {
    //   binaryString[i] = String.fromCharCode(uInt8Array[i]);
    // }
    // var data = binaryString.join('');

    var data = Array.prototype.map.call(uInt8Array, function (ch) {
        return String.fromCharCode(ch);
    }).join('');
    var base64 = window.btoa(data);

    document.getElementById("preview").src="data:image/png;base64,"+base64;
  }
};

xhr.send();