Question

I am trying to upload video file to youtube without refreshing page. I created simple form and try using ajaxSubmit for sending form.

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();
}

File will be uploaded successfully but I don't want redirect page to nexturl params. How to intercept youtube response header and parse location from it ?

Was it helpful?

Solution

Did you check out the example at https://youtube-api-samples.googlecode.com/git/yt-upload-javascript/index.html (source files at https://code.google.com/p/youtube-api-samples/source/browse/#git%2Fyt-upload-javascript) that shows the upload flow, using the Google+ sign-in button to handle OAuth 2 (you can use the normal OAuth 2 browser client flow if you'd prefer) and with a progress indicator.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top