2012-11-02 22 views
5

Tôi đang làm việc trên Phonegap.Làm cách nào để tải ảnh lên sự kiện Facebook qua điện thoại?

Ngay bây giờ tôi đang sử dụng này: -

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

    Some code here 

}, "json"); 
+0

Vui lòng trả lời câu hỏi này, Mã này đang hoạt động cho url hình ảnh web không dành cho Hình ảnh thiết bị của tôi, Nếu tôi sử dụng hình thức multipart/form-data và thay vì duyệt phương thức media cho thiết bị Android. Trình duyệt Android không hỗ trợ loại đầu vào = "tệp". Tôi nên làm gì? –

+0

Bạn có tìm thấy giải pháp nào không? – Brune

Trả lời

0

Có thể thử phương pháp này, nó làm việc cho tôi: (imageData phải là biểu diễn nhị phân của hình ảnh của bạn)

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

Hy vọng điều này giúp! Tạm biệt !

+0

Nó không hoạt động .. Nó đang ném "Đã xảy ra lỗi không xác định". – Brune

0

Tôi hy vọng điều này sẽ hữu ích. Bằng cách tải ảnh lên FB chỉ với sự trợ giúp của javascript, bạn có thể sử dụng phương pháp sau. Điều bắt buộc ở đây là imageData (là định dạng hình ảnh base64) và loại 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

Tôi biết nỗi đau khi không thể tải lên một ảnh. Sau đêm không ngủ và những ngày nghiên cứu, cuối cùng tôi đã nhận nó để làm việc với các plugin cordova tập tin chuyển giao

Đầu tiên thêm plugin: cordova plugin add org.apache.cordova.file-transfer

Sau đó, sử dụng mã này (Lưu ý rằng tôi đang sử dụng angular.js. Người dùng không hứa hẹn hoặc sử dụng thư viện như rsvp hoặc Q để thực hiện lời hứa của bạn):

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

Tôi đã thử điều này. Nó hoạt động, nhưng phải mất mãi để nhận được phản hồi từ chiến thắng. Giống như 60+ giây trên trình giả lập PC. – Brian

Các vấn đề liên quan