2012-11-02 9 views
5

Sto lavorando su PhoneGap.Come caricare le foto sull'evento di Facebook tramite PhoneGap?

In questo momento sto usando questo: -

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

    Some code here 

}, "json"); 
+0

favore mi dia risposta di questa domanda, questo codice sta lavorando per un'immagine web url non per le mie immagini dei dispositivi, se uso forma multipart/form-data e invece del metodo di navigazione del media per dispositivo Android. Il browser Android non supporta il tipo di input = "file". Cosa dovrei fare? –

+0

Hai trovato qualche soluzione? – Brune

risposta

0

Forse provare questo metodo, funziona per me: (imageData devono essere la rappresentazione binaria della vostra immagine)

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); 
     } 
    } 
} 

Spero che questo aiuta! Ciao !

+0

Non funziona. Viene generato "Si è verificato un errore sconosciuto". – Brune

0

Spero che questo sia utile. Effettuando il caricamento delle foto su FB solo con l'aiuto di javascript puoi usare il seguente metodo. La cosa necessaria qui sono imageData (che è il formato base64 dell'immagine) e il tipo 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; 
} 
1

Conosco il dolore di non riuscire a caricare una singola foto. Dopo notti insonni e giorni di ricerca, ho finalmente preso a lavorare con il plugin Cordova trasferimento file

Prima aggiungere il plug-in: cordova plugin add org.apache.cordova.file-transfer

Quindi, utilizzare questo codice (si noti che sto usando angular.js. o non promesse utente o utilizzare una libreria come RSVP o Q per rendere le vostre promesse):

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; 
} 
+0

Ho provato questo. Funziona, ma ci vuole tempo per ottenere una risposta dalla vittoria. Come 60+ secondi su un emulatore PC. – Brian

Problemi correlati