如何通过phonegap在Facebook活动上传照片?

5 cordova

我正在研究Phonegap.

现在我用这个: -

$.post("https://graph.facebook.com/"+Event_Id, { 
     "Photos":uri,
     "access_token": fbAccessToken
},
function(data){

    Some code here

}, "json");
Run Code Online (Sandbox Code Playgroud)

Ant*_*ine 0

也许尝试这个方法,它对我有用:(imageData必须是图像的二进制表示)

function PostImageToFacebook(authToken, filename, mimeType, imageData)
{
    if (imageData != null)
    {
        //Prompt the user to enter a message
        //If the user clicks on OK button the window method prompt() will return entered value from the text box. 
        //If the user clicks on the Cancel button the window method prompt() returns null.
        var message = prompt('Facebook', 'Enter a message');

        if (message != null)
        {
            // this is the multipart/form-data boundary we'll use
            var boundary = '----ThisIsTheBoundary1234567890';

            // let's encode our image file, which is contained in the var
            var formData = '--' + boundary + '\r\n'
            formData += 'Content-Disposition: form-data; name="source"; filename="' + filename + '"\r\n';
            formData += 'Content-Type: ' + mimeType + '\r\n\r\n';
            for (var i = 0; i < imageData.length; ++i)
            {
                formData += String.fromCharCode(imageData[ i ] & 0xff);
            }
            formData += '\r\n';
            formData += '--' + boundary + '\r\n';
            formData += 'Content-Disposition: form-data; name="message"\r\n\r\n';
            formData += message + '\r\n'
            formData += '--' + boundary + '--\r\n';

            var xhr = new XMLHttpRequest();
            xhr.open('POST', 'https://graph.facebook.com/me/photos?access_token=' + authToken, true);
            xhr.onload = xhr.onerror = function() {
            };
            xhr.setRequestHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
            xhr.sendAsBinary(formData);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助 !再见 !