首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >上传Base64图片脸谱图接口

上传Base64图片脸谱图接口
EN

Stack Overflow用户
提问于 2013-04-25 20:07:20
回答 6查看 24.9K关注 0票数 22

我正在尝试使用Node.js将base64图像上传到FaceBook页面。我已经设法让上传工作与所有的多部分数据等,我应该从文件系统读取文件(即。使用fs.readFileSync('c:\a.jpg')

但是,如果我应该使用base64编码的图像并尝试上传它,它会给我以下错误:{"error":{"message":"(#1) An unknown error occurred","type":"OAuthException","code":1}}

我尝试过用new Buffer(b64string, 'base64');将其转换为二进制文件并上传,但没有成功。

我已经在这个问题上挣扎了3天了,所以任何帮助都将不胜感激。

编辑:如果任何人也知道如何将base64转换为二进制文件并成功上传,那对我来说也是有效的。

编辑:代码片段

代码语言:javascript
复制
var postDetails = separator + newlineConstant + 'Content-Disposition: form-data;name="access_token"' + newlineConstant + newlineConstant + accessToken + newlineConstant + separator;

postDetails = postDetails + newlineConstant + 'Content-Disposition: form-data; name="message"' + newlineConstant + newlineConstant + message + newlineConstant;

//Add the Image information
var fileDetailsString = '';
var index = 0;
var multipartBody = new Buffer(0);
images.forEach(function (currentImage) {
    fileDetailsString = fileDetailsString + separator + newlineConstant + 'Content-Disposition: file; name="source"; filename="Image' + index + '"' + newlineConstant + 'Content-Type: image/jpeg' + newlineConstant + newlineConstant;
    index++;

    multipartBody = Buffer.concat([multipartBody, new Buffer(fileDetailsString), currentImage]); //This is what I would use if Bianry data was passed in 

    currentImage = new Buffer (currentImage.toString('base64'), 'base64'); // The following lines are what I would use for base64 image being passed in (The appropriate lines would be enabled/disabled if I was using Binary/base64)
    multipartBody = Buffer.concat([multipartBody, new Buffer(fileDetailsString), currentImage]);
});

multipartBody = Buffer.concat([new Buffer(postDetails), multipartBody, new Buffer(footer)]);
EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2013-11-06 20:05:23

上面的代码对我来说不是很有效(在type:"POST",之后缺少逗号,数据URI到blob函数报告了错误。我得到了以下代码,以便在Firefox和Chrome中工作:

代码语言:javascript
复制
function PostImageToFacebook(authToken)
{
    var canvas = document.getElementById("c");
    var imageData  = canvas.toDataURL("image/png");
    try {
        blob = dataURItoBlob(imageData);
    }
    catch(e) {
        console.log(e);
    }
    var fd = new FormData();
    fd.append("access_token",authToken);
    fd.append("source", blob);
    fd.append("message","Photo Text");
    try {
        $.ajax({
            url:"https://graph.facebook.com/me/photos?access_token=" + authToken,
            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("Posted to facebook");
            }
        });
    }
    catch(e) {
        console.log(e);
    }
}

function dataURItoBlob(dataURI) {
    var byteString = atob(dataURI.split(',')[1]);
    var ab = new ArrayBuffer(byteString.length);
    var ia = new Uint8Array(ab);
    for (var i = 0; i < byteString.length; i++) {
        ia[i] = byteString.charCodeAt(i);
    }
    return new Blob([ab], { type: 'image/png' });
}

这是GitHub https://github.com/DanBrown180/html5-canvas-post-to-facebook-base64的代码

票数 14
EN

Stack Overflow用户

发布于 2013-05-08 19:16:23

我希望这将是有用的。只在javascript的帮助下上传照片到FB,你可以使用以下方法。这里需要的是imageData(这是base64格式的图像)和mime类型。

代码语言:javascript
复制
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;
}

//编辑AJAX语法

票数 24
EN

Stack Overflow用户

发布于 2014-03-18 07:29:22

丹的答案效果最好。在这个场景中,另一个可能有用的东西是用于发布照片的可选参数:'no_story‘。这个参数缺省为true,强制照片发布跳过用户的墙。通过添加

代码语言:javascript
复制
fd.append("no_story", false);

您可以使用照片帖子更新用户的墙。

我本想留下这段评论的,但是...50名代表发表评论。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16214300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档