Skip to content

Instantly share code, notes, and snippets.

@andrew-aladev
Created November 19, 2012 14:31
Show Gist options
  • Save andrew-aladev/4110934 to your computer and use it in GitHub Desktop.
Save andrew-aladev/4110934 to your computer and use it in GitHub Desktop.
facebook image share
if (XMLHttpRequest.prototype.sendAsBinary === undefined) {
XMLHttpRequest.prototype.sendAsBinary = function(string) {
var bytes = Array.prototype.map.call(string, function(c) {
return c.charCodeAt(0) & 0xff;
});
this.send(new Uint8Array(bytes).buffer);
};
}
FB.PostImage = function(authToken, data, message) {
var boundary = "----image" + new Date().valueOf();
form = "--" + boundary + "\r\n";
form += "Content-Disposition: form-data; name=\"source\"; filename=\"image.png\"\r\n";
form += "Content-Type: image/png\r\n\r\n";
form += data;
form += "\r\n";
form += "--" + boundary + "--\r\n";
var xhr = new XMLHttpRequest();
xhr.open("POST", "https://graph.facebook.com/me/photos?access_token=" + authToken + "&message=" + message, true);
xhr.onload = function() {
console.log(xhr.responseText);
};
xhr.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
xhr.sendAsBinary(form);
}
var data = atob($("#canvas-container canvas").last().get(0).toDataURL("image/png").replace("data:image/png;base64,", ""));
FB.login(function(response) {
if (response.authResponse) {
FB.PostImage(response.authResponse.accessToken, data, "test case");
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment