问题
I am trying to make a browser based youtube video uploader using javascript.
I am using sample code from here
After authentication when I upload video, POST request to youtube server never ends and video is not uploaded.
This is happening in the sample provided by google too.
Here is the function that i am using to upload video:
$('#upload').click(function(){
$('#upload').attr('disabled', true);
var title = escapeXmlEntities($('#title').val());
var description = escapeXmlEntities($('#description').val());
var category = escapeXmlEntities($('#category option:selected').val());
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>';
showMsg("Submitting metadata of video to get upload token.");
$.ajax({
dataType: 'xml',
type: 'POST',
url: 'https://gdata.youtube.com/action/GetUploadToken',
contentType: 'application/atom+xml; charset=UTF-8',
processData: false,
headers: generateYouTubeApiHeaders(),
data: xmlBody,
success: function(responseXml) {
var xml = $(responseXml);
var nextUrl = window.location.href;
var submissionUrl = xml.find('url').text() + '?nexturl=' + encodeURIComponent(nextUrl);
var token = xml.find('token').text();
showMsg("Uploading Video...");
$('#form_upload').attr('action', submissionUrl);
$('<input>').attr({
type: 'hidden',
name: 'token',
value: token
}).appendTo('#form_upload');
$('#form_upload').submit();
},
error: function(jqXHR) {
showMsg('Metadata submission failed: ' + jqXHR.responseText);
$('#upload').removeAttr('disabled');
$('#upload').val('Upload');
}
});
});
Here is the firebug screenshot of the last POST request made to youtube server that is never ending.
Moreover I want to get the progress of the video upload and show it using javascript but I don't know how to achieve it I googled and found many methods but nothing was successful for me.
回答1:
There's now support for resumable uploads using CORS in the YouTube Data API v3.
A rough, but working, 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. It also shows how you could poll for video processing status following an upload and embed the resulting video on a page once it's been processed.
来源:https://stackoverflow.com/questions/19725673/youtube-video-upload-using-javascript