I'm trying to write canvas data as an image (png) to my dropbox. I manage to get the data from canvas and to save a file to dropbox, but the file is not an image file it seams.
According to the documentation the image data should be converted to a arrayBuffer. That I'm doing using a function found here on Stackoverflow but something doesn't seem to work. Does anyone know what I'm doing wrong?
function _str2ab(str) {
var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char
var bufView = new Uint16Array(buf);
for (var i=0, strLen=str.length; i<strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
function _savePicture () {
//Get data from canvas
var imageSringData = canvas.toDataURL('image/png');
//Convert it to an arraybuffer
var imageData = _str2ab(imageSringData);
client.writeFile('/Public/the_image.png', imageData, function(error, stat) {
if (error) {
console.log('Error: ' + error);
} else {
console.log('File written successfully!');
}
});
Here's some dropbox documentation. https://github.com/dropbox/dropbox-js/blob/stable/guides/snippets.md