Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mingliangguo/e7042a504ef68720024d37427ea58898 to your computer and use it in GitHub Desktop.
Save mingliangguo/e7042a504ef68720024d37427ea58898 to your computer and use it in GitHub Desktop.
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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment