Youtube API直接上载和返回URL,可能在Ajax中? [英] Youtube API Direct Upload and Return URL, Possibly in Ajax?

查看:159
本文介绍了Youtube API直接上载和返回URL,可能在Ajax中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,这是情况.

我已经有Youtube API可以上传视频了.上传后,它会返回 http://example.com/?status=200& id = 3-1234xyz .

I've got the Youtube API working to upload a video. Once it is uploaded, it returns http://example.com/?status=200&id=3-1234xyz.

理想情况下,我想将此内容提交并通过Ajax上传,然后返回ID以立即显示在页面上(最终将显示视频的URL或嵌入代码).我正在使用jquery,但在所有这些方面我都处于末日.任何帮助,链接等都将不胜感激!

Ideally, I'd like to have this submitted and uploaded via Ajax and then have the ID returned to immediately display on the page (that will eventually show the URL or embed code for the video). I'm using jquery, but I'm at my whits end with all of this. Any and all help, links, etc are greatly appreciated!

推荐答案

使用iframe.您只需将文件上传表单发布到iframe中,然后完成加载(您可以使用$('#iframe').ready(...)捕获此事件),然后可以捕获视频ID(因为它位于同一域).

Use an iframe. You simply post the file upload form into the iframe, when it's then finished loading (you can capture this event with $('#iframe').ready( ... )) you can then capture the video id (cos it's on the same domain).

因此,您的上传表单将遵循以下几行:

So your upload form will be along the following lines:

<form action="...?nexturl=IFRAME_URL" method="post" enctype="multipart/form-data" target="uploader">
<p>Please select the video you wish to upload to YouTube:</p>
<input type="file" name="file" value="" />
<input type="hidden" name="token" value="" />
<button>Upload</button>
</form>
<iframe id="uploader" name="uploader" style="display: none; width: 1px; height: 1px; border: none;"></iframe>

然后您所有的iframe要做的就是将视频ID传递给父级(或者您可以从父级中读取它,但我更喜欢前者):

And then all your iframe needs to do is just pass the video id up to the parent (or you can read it from the parent, but I prefer the former):

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script type="text/javascript">
    <!--
        var vars    = getVars();

        $( document ).ready( function()
        {
            parent.selectVideo( vars[ 'id' ] );
        });

        function getVars()
        {
            var vars = [];
            var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
            var hash;

            for(var i = 0; i < hashes.length; i++)
            {
                hash = hashes[i].split('=');

                vars.push(hash[0]);
                vars[hash[0]] = hash[1];
            }

            return vars;
        }
    -->
    </script>
    <title></title>
</head>
<body>
</body>

完成工作:D

这篇关于Youtube API直接上载和返回URL,可能在Ajax中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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