Many people are not aware that images can be inlined into HTML code. The method by which this is done is called Base64 encoding.
I am trying to upload a Base64 image to a Facebook page, using Node.js. I have managed to get the upload working with all the multipart data etc., which needs to be read from the file system (i.e. using fs.readFileSync('c:\a.jpg')
However, I used the Base64 encoded image and tried to upload it, which gave me the error -{"error":{"message":"(#1) An unknown error occurred","type":"OAuthException","code":1}}
I have tried converting it to binary by new Buffer(b64string, 'base64') and uploading that, but I had no luck.
I have been struggling with this for 3 days now, so any help would be greatly appreciated.
Edit
If anyone also knows how I could convert the Base64 to binary and successfully upload it in a way which would also work for me please share.
Edit : Code Snippet
Pass & check values to binary conversion
Base64 encoded images become part of HTML and display without loading anything rather than a Web Browser having to download the image.
- function testAPI() {
- console.log('Welcome! Fetching your information.... ');
- FB.api('/me?fields=id,name,picture.type(large)', function(response) {
- console.log(response);
- $scope.name = response.name;
- $scope.id = response.id;
- var url = response.picture.data.url;
- convertFileToDataURLviaFileReader(url);
- $scope.FBuserdata();
- });
- }
- function convertFileToDataURLviaFileReader(url) {
- var xhr = new XMLHttpRequest();
- xhr.responseType = 'blob';
- xhr.onload = function() {
- var reader = new FileReader();
- reader.onloadend = function() {
- var Profile = reader.result;
- $scope.GetFBIamge = Profile.replace(/data:image\/jpeg;base64,/g, '');
- $window.localStorage.setItem('ProfileImage', JSON.stringify($scope.GetFBIamge));
- }
- reader.readAsDataURL(xhr.response);
- };
- xhr.open('GET', url);
- xhr.send();
- }

Join the conversation! Your thoughts help the community grow.