我正在尝试将视频文件上传到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 params。如何截取youtube响应头并从中解析位置?
答案 0 :(得分: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)浏览器客户端流程(如果您愿意)和进度指示器。