javascript - Upload Base64 Image Facebook Graph API -
i'm trying upload base64 image facebook page using node.js. have managed upload working multipart data etc should read file filesystem (ie. using fs.readfilesync('c:\a.jpg')
however, should use base64 encoded image , try upload it, give me following error : {"error":{"message":"(#1) unknown error occurred","type":"oauthexception","code":1}}
i have tried converting binary new buffer(b64string, 'base64');
, uploading that, no luck.
i have been struggling 3 days now, anyhelp appreciated.
edit : if knows how convert base64 binary , upload it, work me.
edit : code snippet
var postdetails = separator + newlineconstant + 'content-disposition: form-data;name="access_token"' + newlineconstant + newlineconstant + accesstoken + newlineconstant + separator; postdetails = postdetails + newlineconstant + 'content-disposition: form-data; name="message"' + newlineconstant + newlineconstant + message + newlineconstant; //add image information var filedetailsstring = ''; var index = 0; var multipartbody = new buffer(0); images.foreach(function (currentimage) { filedetailsstring = filedetailsstring + separator + newlineconstant + 'content-disposition: file; name="source"; filename="image' + index + '"' + newlineconstant + 'content-type: image/jpeg' + newlineconstant + newlineconstant; index++; multipartbody = buffer.concat([multipartbody, new buffer(filedetailsstring), currentimage]); //this use if bianry data passed in currentimage = new buffer (currentimage.tostring('base64'), 'base64'); // following lines use base64 image being passed in (the appropriate lines enabled/disabled if using binary/base64) multipartbody = buffer.concat([multipartbody, new buffer(filedetailsstring), currentimage]); }); multipartbody = buffer.concat([new buffer(postdetails), multipartbody, new buffer(footer)]);
the code above didn't quite work me (missing comma after type:"post",
, data uri blob function reported errors. got following code work in firefox , chrome:
function postimagetofacebook(authtoken) { var canvas = document.getelementbyid("c"); var imagedata = canvas.todataurl("image/png"); try { blob = datauritoblob(imagedata); } catch(e) { console.log(e); } var fd = new formdata(); fd.append("access_token",authtoken); fd.append("source", blob); fd.append("message","photo text"); try { $.ajax({ url:"https://graph.facebook.com/me/photos?access_token=" + authtoken, type:"post", data:fd, processdata:false, contenttype:false, cache:false, success:function(data){ console.log("success " + data); }, error:function(shr,status,data){ console.log("error " + data + " status " + shr.status); }, complete:function(){ console.log("posted facebook"); } }); } catch(e) { console.log(e); } } function datauritoblob(datauri) { var bytestring = atob(datauri.split(',')[1]); var ab = new arraybuffer(bytestring.length); var ia = new uint8array(ab); (var = 0; < bytestring.length; i++) { ia[i] = bytestring.charcodeat(i); } return new blob([ab], { type: 'image/png' }); }
here's code @ github https://github.com/danbrown180/html5-canvas-post-to-facebook-base64