Kashif
Kashif

Reputation: 11

upload photo on facebook using javascript

I am trying to upload photo on facebook using javascript. I was able to do status update doing javascript but still struggling with how to upload photo. Can someone please tell me how to code this in Javascript?

Upvotes: 1

Views: 5700

Answers (3)

mcbjam
mcbjam

Reputation: 7454

Here is an example code :

var imgURL = 'URL de la photo a uploader';

FB.api('/ALBUM_ID/photos', 'post', {
    message: ' Ma photo',
    url: imgURL,
}, function (response) { 

});

Upvotes: 0

Brune
Brune

Reputation: 1060

I hope this will be useful. By doing photo upload to FB only with the help of javascript you can use the following method. Required thing here are imageData(which is base64 format of image) and the mime type.

try{
        blob = dataURItoBlob(imageData,mimeType);
}catch(e){console.log(e);}
var fd = new FormData();
fd.append("access_token",accessToken);
fd.append("source", blob);fd.append("message","Kiss");
try{
   $.ajax({
        url:"https://graph.facebook.com/" + <<userID received on getting user details>> + "/photos?access_token=" + <<user accessToken>>,
        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("Ajax Complete");
        }
    });

}catch(e){console.log(e);}

function dataURItoBlob(dataURI,mime) {
    // convert base64 to raw binary data held in a string
    // doesn't handle URLEncoded DataURIs

    var byteString = window.atob(dataURI);

    // separate out the mime component


    // write the bytes of the string to an ArrayBuffer
    //var ab = new ArrayBuffer(byteString.length);
    var ia = new Uint8Array(byteString.length);
    for (var i = 0; i < byteString.length; i++) {
        ia[i] = byteString.charCodeAt(i);
    }

    // write the ArrayBuffer to a blob, and you're done
    var blob = new Blob([ia], { type: mime });

    return blob;
}

Upvotes: 0

andyrandy
andyrandy

Reputation: 73984

The correct code would be:

var params = {};
params.url = 'https://myserver.com/myimage.jpg';

FB.api('/me/photos', 'post', params, function(response) {
    if (!response || response.error) {
        //error
    } else {
        picid = response.id;
    }
}); 

Keep in mind that the photo has to be on your server, so you need a server script for the upload. the "url" parameter is the absolute url of your uploaded image file. More information: https://developers.facebook.com/docs/reference/api/user/ (see Photos/Create)

Keep in mind that the "message" parameter has to be 100% user generated according to the Facebook terms. You also can´t post to a friend wall of the logged in user, that functionality is deprecated and does not work anymore.

Upvotes: 3

Related Questions