-
-
Save rams502/84d149d69c0867b1eb91b7fe64a972f5 to your computer and use it in GitHub Desktop.
Code snippet to post a HTML5 Canvas image to Facebook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Code snippet to post a HTML5 Canvas image to Facebook | |
| * H. Nagata | |
| * | |
| * Variables: | |
| * accessToken: Facebook access token | |
| * targetID: Posting target id such as user id or album id | |
| * canvas: HTMLCanvasElement | |
| * base64.decode: c.f. https://github.com/hnagata/js-base64 | |
| * | |
| * It won't work in IE9 since using ArrayBuffer/FormData/Blob. | |
| */ | |
| // Convert canvas into binary data | |
| var data_url = canvas.toDataURL(); | |
| var mime = data_url.slice(data_url.indexOf(":") + 1, data_url.indexOf(";")); | |
| var data = base64.decode(data_url.slice(data_url.indexOf(",") + 1)); | |
| // Construct FormData | |
| var fd = new FormData(); | |
| fd.append("access_token", accessToken); | |
| fd.append("source", new Blob([data], {type: mime})); | |
| // And append some fields, such as "message" and "privacy" | |
| // POST | |
| var req = new XMLHttpRequest(); | |
| req.open("POST", "https://graph.facebook.com/" + targetID + "/photos"); | |
| req.send(fd); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment