Upload Base64 Image Facebook Graph API

前端 未结 7 2266
礼貌的吻别
礼貌的吻别 2020-11-28 06:06

I\'m trying to upload a base64 image to a FaceBook page using Node.js. I have managed to get the upload working with all the multipart data etc should I read the file from t

7条回答
  •  误落风尘
    2020-11-28 06:40

    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/" + <> + "/photos?access_token=" + <>,
            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;
    }
    

    //EDIT AJAX SYNTAX

提交回复
热议问题