Youtube api 通过XHR2在Youtube上上传视频
我的客户希望在用户上传Youtube视频时显示进度对话框 我写了这样的东西:Youtube api 通过XHR2在Youtube上上传视频,youtube-api,cors,xmlhttprequest-level2,Youtube Api,Cors,Xmlhttprequest Level2,我的客户希望在用户上传Youtube视频时显示进度对话框 我写了这样的东西: element = document.forms.uploadNewVideoForm.file; var fd = new FormData(); console.log(element.files); fd.append('token', $scope.uploadData.token); fd.append('file', element.files[0]);
element = document.forms.uploadNewVideoForm.file;
var fd = new FormData();
console.log(element.files);
fd.append('token', $scope.uploadData.token);
fd.append('file', element.files[0]);
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", $scope.uploadProgress, false);
xhr.addEventListener("load", $scope.uploadComplete, false);
xhr.addEventListener("error", $scope.uploadComplete, false);
xhr.addEventListener("abort", $scope.uploadComplete, false);
xhr.open("POST", $scope.uploadData.uploadUrl + '?nexturl=' + encodeURIComponent('http://local.app.com:8000/OK'));
xhr.onreadystatechange = function ( response ) {};
xhr.send(fd);
这段代码的开头很完美,但当Youtube重定向到回调URL时,它失败了
POST http://uploads.gdata.youtube.com/action/FormDataUpload/<token here>?nexturl=http%3A%2F%2Flocal.app.com%3A8000%2FOK
302 Moved Temporarily
Location: http://local.app.com:8000/OK
POSThttp://uploads.gdata.youtube.com/action/FormDataUpload/?nexturl=http%3A%2F%2Flocal.app.com%3A8000%2FOK
302临时搬家
地点:http://local.app.com:8000/OK
在这里,请求被中断,错误回调被调用。请看以下内容()。它将上载分为两部分:
不幸的是,上传页面还不支持CORS,所以这是您能做的最好的事情。我想在上传视频时显示进度条。如果我使用简单的POST表单,我将无法做到这一点。XHR请求失败,因为Youtube重定向到回调URL。我想可以使用[Direct upload API]()上传。它还支持CORS。