Save image to dropbox with data from canvas

拜拜、爱过 提交于 2019-12-02 07:54:39

Got it to work finally! I used another function to convert base64 to bufferArray and remove the 'data:image/png;base64,' from the string and it worked. Hopefully this will help someone else in the future.

function _base64ToArrayBuffer(base64) {
    base64 = base64.split('data:image/png;base64,').join('');
    var binary_string =  window.atob(base64),
        len = binary_string.length,
        bytes = new Uint8Array( len ),
        i;

    for (i = 0; i < len; i++)        {
        bytes[i] = binary_string.charCodeAt(i);
    }
    return bytes.buffer;
}

function _savePicture () {
    //Get data from canvas
    var imageSringData = canvas.toDataURL('image/png');
    //Convert it to an arraybuffer
    var imageData = _base64ToArrayBuffer(imageSringData);

    client.writeFile('/Public/the_image.png', imageData, function(error, stat) {
    if (error) {
        console.log('Error: ' + error);
    } else {
        console.log('File written successfully!');
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!