我正在研究 Phonegap 。
现在我正在使用这个: -
$.post("https://graph.facebook.com/"+Event_Id, {
"Photos":uri,
"access_token": fbAccessToken
},
function(data){
Some code here
}, "json");
我正在研究 Phonegap 。
现在我正在使用这个: -
$.post("https://graph.facebook.com/"+Event_Id, {
"Photos":uri,
"access_token": fbAccessToken
},
function(data){
Some code here
}, "json");
我知道无法上传单张照片的痛苦。经过不眠之夜和几天的研究,我终于让它与科尔多瓦文件传输插件一起工作
首先添加插件:cordova plugin add org.apache.cordova.file-transfer
然后,使用此代码(请注意,我使用的是 angular.js。不要使用用户承诺或使用 rsvp 或 Q 之类的库来做出承诺):
function postImage(fileURI, message) {
var deferred = $q.defer();
var win = function (r) {
deferred.resolve(r);
}
var fail = function (error) {
deferred.reject(error);
}
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = 'name_of_photo_' + Math.round((+(new Date()) + Math.random()));
options.mimeType = "image/jpg";
var params = new Object();
params.access_token = "your facebook access token ;)";
params.message = message;
params.no_story = false;
options.params = params;
var ft = new FileTransfer();
ft.upload(fileURI, "https://graph.facebook.com/v2.0/me/photos", win, fail, options);
return deferred.promise;
}
也许试试这个方法,它对我有用:(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);
}
}
}
希望这可以帮助 !再见 !
我希望这会有用。通过仅在 javascript 的帮助下将照片上传到 FB,您可以使用以下方法。这里需要的东西是imageData(这是base64格式的图像)和mime类型。
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;
}