0

我正在尝试在不刷新页面的情况下将视频文件上传到 youtube。我创建了简单的表单并尝试使用 ajaxSubmit 发送表单。

function prepareUploadForm() {
    $('#upload').click(function() {
        $('#upload').attr('disabled', true);
        $('#upload').val('Uploading...');
        var title = escapeXmlEntities($('#firstname').val()+" "+$('#lastname').val());
        var description = escapeXmlEntities($('#firstname').val()+" "+$('#lastname').val()+' intro video');
        var category = escapeXmlEntities('Sports');
        var xmlBody = '<'+'?xml version="1.0"?>'+'<entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007"> <media:group> <media:title type="plain">' + title + '</media:title> <media:description type="plain">' + description + '</media:description> <media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">' + category + '</media:category> </media:group> </entry>';
        $.ajax({
            dataType: 'xml',
            type: 'POST',
            url: 'https://gdata.youtube.com/action/GetUploadToken',
            contentType: 'application/atom+xml; charset=UTF-8',
            headers: generateYouTubeApiHeaders(),
            processData: false,
            data: xmlBody,
            success: function(responseXml) {
                var xml = $(responseXml);
                var nextUrl = window.location.href+"?task=user.youtubeFileUploadParse";
                var submissionUrl = xml.find('url').text() + '?nexturl=' + encodeURIComponent(nextUrl);
                var token = xml.find('token').text();
                console.log("send data to: "+xml.find('url').text());
                console.log(submissionUrl);
                console.log('token:' +token);
                submiss = submissionUrl;
                $('#upload-form').attr('action', xml.find('url').text());
                $('<input>').attr({
                    type: 'hidden',
                    name: 'token',
                    value: token
                }).appendTo('#upload-form');

                //implement ajax file upload to youtube with upload token
                var options = {
                    url: submissionUrl,
                    success:    function(response) {
                        console.log('Thanks for your file!');
                    }
                };
                $('#upload-form').ajaxSubmit(options);
//                $('#upload-form').submit();
            },
            error: function(jqXHR) {
                console.log('Metadata submission failed: ' + jqXHR.responseText);
                $('#upload').removeAttr('disabled');
                $('#upload').val('Upload');
            }
        });
    });
    $('#file-video').change(function() {
        console.log('change video file');
        if ($(this).val()) {
            $('#upload').removeAttr('disabled');
        } else {
            $('#upload').attr('disabled', true);
        }
    });
    $('#upload-form').show();
}

文件将成功上传,但我不希望将页面重定向到 nexturl 参数。如何拦截 youtube 响应标头并从中解析位置?

4

1 回答 1

2

您是否查看了 https://youtube-api-samples.googlecode.com/git/yt-upload-javascript/index.html 上的示例源文件位于https://code.google.com/p/youtube- api-samples/source/browse/#git%2Fyt-upload-javascript)显示上传流程,使用 Google+ 登录按钮处理 OAuth 2(如果您愿意,可以使用普通的 OAuth 2 浏览器客户端流程) 并带有进度指示器。

于 2013-11-11T16:53:37.130 回答