I am trying to use the YouTube API to perform a browser-based upload, but also use Ajax for the purpose of showing a progress bar.
I started off here: https://developers.google.com/youtube/2.0/developers_guide_protocol_browser_based_uploading
It works 100% if I just use the basic HTML form post.
<form id="frmYouTube" enctype="multipart/form-data" method="post" action="https://uploads.gdata.youtube.com/action/FormDataUpload/YOUTUBE_URL_HERE?nexturl=https%3a%2f%2fdomain%2fpageafter%2fVideoUploadDone%3fid2%3dLOCAL_ID">
<table>
<tr>
<td><input type="file" name="file" /></td>
</tr>
<tr>
<td>
<input type="hidden" name="token" value="YOUTUBE_TOKEN_HERE" />
<input type="submit" value="Upload" />
</td>
</tr>
</table>
</form>
However if I try to add some Javascript I run into a problem. I'm also including MooTools and the Request.File class from http://mootools.net/forge/p/form_upload.
Here is a simplified version of the code:
var yt = $('frmYouTube');
var ytDone = function () {
// Code to display: 'Video upload complete. Return to <a href="/Acount">your account</a>.';
};
var ytProgress = function (event, xhr) {
var loaded = event.loaded, total = event.total;
// Code to display: 'Uploading... ' + (parseInt(loaded / total * 100, 10)) + '%';
};
yt.addEvents({ 'submit': function (ev) {
ev.stop();
var rf = new Request.File({
url: yt.get('action'),
onProgress: ytProgress,
onFailure: function (xhr) {
ytError('Upload Failed (1).');
},
onCancel: function () {
ytError('Upload Canceled.');
},
onException: function () {
ytError('Upload Failed (2).');
},
onSuccess: ytDone
});
yt.getElements('input').each(function (field) {
if (field.files) {
rf.append(field.get('name'), field.files[0]);
} else {
rf.append(field.get('name'), field.get('value'));
}
});
rf.send();
}
});
</script>
At this point the file gets uploaded successfully, and YouTube returns a 302 redirect to my "nexturl," but because of cross-origin rules the redirect is not followed and I can not access the location header using Javascript.
I've seen a few solutions that involved iframes, but that won't work if you want a progress bar. Has anyone come up with a work-around to do browser based uploads to YouTube and show a progress bar.