我的客户希望在用户在 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]);
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
这里请求中断并调用错误回调。