Upload Base64 Image Facebook Graph API

前端 未结 7 2120
礼貌的吻别
礼貌的吻别 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:41

    Here is an example that does not need jQuery or other libraries, just the native Fetch API:

    const dataURItoBlob = (dataURI) => {
        let byteString = atob(dataURI.split(',')[1]);
        let ab = new ArrayBuffer(byteString.length);
        let ia = new Uint8Array(ab);
        for (let i = 0; i < byteString.length; i++) {
            ia[i] = byteString.charCodeAt(i);
        }
        return new Blob([ia], {
            type: 'image/jpeg'
        });
    }
    const upload = async (response) => {
        let canvas = document.getElementById('canvas');
        let dataURL = canvas.toDataURL('image/jpeg', 1.0);
        let blob = dataURItoBlob(dataURL);
        let formData = new FormData();
        formData.append('access_token', response.authResponse.accessToken);
        formData.append('source', blob);
    
        let responseFB = await fetch(`https://graph.facebook.com/me/photos`, {
            body: formData,
            method: 'post'
        });
        responseFB = await responseFB.json();
        console.log(responseFB);
    };
    document.getElementById('upload').addEventListener('click', () => {
        FB.login((response) => {
            //TODO check if user is logged in and authorized publish_actions
            upload(response);
        }, {scope: 'publish_actions'})
    })
    

    Source: http://www.devils-heaven.com/facebook-javascript-sdk-photo-upload-from-canvas/

    0 讨论(0)
提交回复
热议问题