I'm trying to convert the response of the dropbox get_thumbnail api to an actual img tag in my page, actually the response returned from the api has a shape like the following
����JFIF��C %# , #&')*)-0-(0%()(��C (((((((((((((((((((((((((((((((((((((((((((((((((((��@0"�� ���}!1AQa"q2���#B��R��$3br� %&'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz��������������������������������������������������������������������������� ���w!1AQaq"2�B���� #3R�br� $4�
This is what I've tried to convert that response in an img element:
var imgsrc = 'data:image/jpeg;base64,' + hexToBase64(data);
var img = new Image(100, 100);
img.src = imgsrc;
document.body.appendChild(img);
function hexToBase64(str) {
return btoa(String.fromCharCode.apply(null, str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" ")));
}
Where the data variable passed to the hexToBase64 function contain the server response but it did not work (this solution is just a copy-paste of code retrieved in another stackoverflow question).
Any help is very appreciated.
Thanks.