Facebook新的javascript sdk-上传照片与它! [英] Facebook new javascript sdk- uploading photos with it!

查看:80
本文介绍了Facebook新的javascript sdk-上传照片与它!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  FB.api('/ me /照片','post',{access_token:GetToken(),
name:'uploaded photo',
source:'@http://example.com/example.jpg'},
函数(响应){
if(!response || response.error){
alert('Error occured'+ response.error.message);
} else {
alert('Post Id:'+ response.id);
}
});

有人可以帮助我这个代码。这个代码没有返回任何东西。

解决方案

假设你想在纯JavaScript / JQuery中这样做 - 你需要使用一个iframe作为表单的目标,有一个警告 - 由于相同的起始策略,您将无法使用返回数据(呼叫的ID /成功)。



首先创建一个表单,将保存文件输入和您想要设置的任何其他变量:

  form id =upload-photo-form> 
< input name =sourceid =sourcesize =27type =file/>
< input name =messageid =messagetype =textvalue =message example please ignore/>
< / form>

这是创建iframe的主要功能,指向使用它的表单,然后检索来自专辑的最新照片使用FQL。

  function fileUpload(form,action_url,div_id){
//创建一个iframe
var iframe = document.createElement(iframe);
iframe.setAttribute('id',upload_iframe);
iframe.setAttribute('name',upload_iframe);
iframe.setAttribute('width',0px);
iframe.setAttribute('height',0px);
iframe.setAttribute('border',0);
iframe.setAttribute('style','width:0; height:0; border:none;);

//添加到文档。
form.parentNode.appendChild(iframe);
window.frames ['upload_iframe']。name =upload_iframe;

iframeId = document.getElementById(upload_iframe);

//添加事件以检测上传完成后
var eventHandler = function(){

if(iframeId.detachEvent)
{
iframeId.detachEvent(onload,eventHandler);
}
else
{
iframeId.removeEventListener(load,eventHandler,false);
}

setTimeout(function(){
try
{
$('#upload_iframe')。remove();
} catch(e){

}
},100);

FB.api({
method:'fql.query',
query:'SELECT src_big,pid,caption,object_id FROM photo WHERE aid ='+ albumID + 'ORDER BY创建DESC LIMIT 1'
},
函数(响应){
console.log(响应);
}
);

}

if(iframeId.addEventListener)
iframeId.addEventListener('load',eventHandler,true);
if(iframeId.attachEvent)
iframeId.attachEvent('onload',eventHandler);

//设置窗体的属性
form.setAttribute('target','upload_iframe');
form.setAttribute('action',action_url);
form.setAttribute('method','post');
form.setAttribute('enctype','multipart / form-data');
form.setAttribute('encoding','multipart / form-data');

//提交表单...
form.submit();

}

在运行时您可能会从上一次调用中了解albumObjectID ,并且具有通过登录或会话onauthchange返回的会话对象的访问令牌。

  var url ='https:/ /graph.facebook.com/'+ albumObjectID +'/ photos?access_token ='+ accessToken; 
fileUpload($('#upload-photo-form')[0],url,$('#upload-photo-div')[0]);`

显然这不是生产代码,有几件事情可以改善它的可靠性(如检查宽度,高度,标题和尺寸)提交的图像的标签到最新的图像)。检查最新图像之前&尝试上传之后等等。



PS:注意专辑对象ID和相册ID,但它们是不同的,但两者都可以使用一些简单的FQL查询获得。 (例如: SELECT aid,object_id,can_upload,name FROM album WHERE owner = me()AND name =My Album Name



希望这有帮助。

CameraSchoolDropout


I am trying to upload a photo to facebook album with this javaascript code.

FB.api('/me/photos', 'post', {    access_token: GetToken(),
                            name: 'uploaded photo',
                            source: '@http://example.com/example.jpg' }, 
            function(response) {
                if (!response || response.error) {
                    alert('Error occured ' + response.error.message);
                } else {
                    alert('Post Id: ' + response.id);
                }
            });

Can someone help me with this code. This code is not returning anything.

解决方案

Assuming you want to do this in pure JavaScript/JQuery - you'll need to use an iframe as the target of a form, there is a caveat - you will not be able to use the return data (the ID / success of the call) because of the same origin policy.

First create a form that will hold the file input and any other variables you wish to set:

<form id="upload-photo-form">
    <input name="source" id="source" size="27" type="file" />
    <input name="message" id="message" type="text" value="message example please ignore" />
</form>

Here is the main function used which creates an iframe, points the form to use it, and then retrieves the latest photo from the album using FQL.

function fileUpload(form, action_url, div_id) {
    // Create an iframe 
    var iframe = document.createElement("iframe");
    iframe.setAttribute('id', "upload_iframe");
    iframe.setAttribute('name', "upload_iframe");
    iframe.setAttribute('width', "0px");
    iframe.setAttribute('height', "0px");
    iframe.setAttribute('border', "0");
    iframe.setAttribute('style', "width: 0; height: 0; border: none;");

    // Add to document.
    form.parentNode.appendChild(iframe);
    window.frames['upload_iframe'].name = "upload_iframe";

    iframeId = document.getElementById("upload_iframe");

    // Add event to detect when upload has finished
    var eventHandler = function () {

        if (iframeId.detachEvent)
        {
            iframeId.detachEvent("onload", eventHandler);
        }
        else
        {
            iframeId.removeEventListener("load", eventHandler, false);
        }

        setTimeout(function() {
            try
            {
                $('#upload_iframe').remove();
            } catch (e) {

            }
        }, 100);

        FB.api({
            method: 'fql.query',
            query: 'SELECT src_big,pid,caption,object_id FROM photo WHERE aid= "' + albumID + '" ORDER BY created DESC LIMIT 1'
            },
            function(response) {
                console.log(response);
            }
        );

    }

    if (iframeId.addEventListener)
        iframeId.addEventListener('load', eventHandler, true);
    if (iframeId.attachEvent)
        iframeId.attachEvent('onload', eventHandler);

    // Set properties of form...
    form.setAttribute('target', 'upload_iframe');
    form.setAttribute('action', action_url);
    form.setAttribute('method', 'post');
    form.setAttribute('enctype', 'multipart/form-data');
    form.setAttribute('encoding', 'multipart/form-data');

    // Submit the form...
    form.submit();

}   

At runtime you will presumably know the albumObjectID from a previous call, and have the access token from the session object that is returned by login or session onauthchange.

var url = 'https://graph.facebook.com/' + albumObjectID + '/photos?access_token=' +  accessToken;
fileUpload($('#upload-photo-form')[0], url, $('#upload-photo-div')[0]);`

Obviously this isn't production code, there are a few things you can do to improve it's reliability (like checking the width, height, caption & tags of the submitted image to the latest image). Checking the latest image before & after the attempted upload etc.

PS: Watch out for the albumObjectID vs albumID, they are different however both can be obtained using some simple FQL queries. (eg: SELECT aid, object_id, can_upload, name FROM album WHERE owner = me() AND name = "My Album Name")

Hope this helps.
CameraSchoolDropout

这篇关于Facebook新的javascript sdk-上传照片与它!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆