【发布时间】:2015-07-29 21:35:21
【问题描述】:
我想从我的手机设备存储中将视频上传到 YouTube。但是,当我上传文件时,它是空白的。当我使用相同的上传代码但使用网络文件时,它可以工作。想知道我哪里错了!
方法一 一切都正确上传,视频在 YouTube 上播放。
loadWebFile('assets/intro.mpg');
function loadWebFile(url) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'blob';
xhr.onload = function (e) {
uploadFile(xhr.response); // type: Blob
};
xhr.onerror = function (e) {
console.log('loadWebFile.onerror', e);
};
xhr.send();
};
方法二 视频标题和说明出现在 YouTube 上,但视频是空白的。我肯定通过了一个有效的文件。
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
if (window.webkitStorageInfo) {
window.webkitStorageInfo.requestQuota(access, 1024 * 1024, function (bytes) {
if (window.requestFileSystem) {
window.requestFileSystem(access, bytes, function (filesystem) {
loadFile('/Movies/intro.mpg');
}, me.onError);
} else {
window.alert('requestFileSystem not supported');
}
}, me.onError);
} else {
window.alert('webkitStorageInfo not supported');
}
// this sends an empty video to YouTube
function loadFile(path) {
filesystem.root.getFile(path, null, function (fileEntry) {
fileEntry.file(function (file) {
uploadFile(file); // type: File
});
}, function (e) {
console.log('loadFile.error', e);
});
}
两种方法共享相同的上传功能:
// uploads using the YouTube script
// https://github.com/youtube/api-samples/blob/master/javascript/cors_upload.js
function uploadFile(file) {
var metadata = {
snippet: {
title: 'Video title',
description: 'Video description',
tags: 'Video tags',
categoryId: 22
},
status: {
privacyStatus: 'unlisted'
}
};
var uploader = new MediaUploader({
baseUrl: 'https://www.googleapis.com/upload/youtube/v3/videos',
file: file,
token: accessToken,
metadata: metadata,
params: {
part: Object.keys(metadata).join(',')
},
onError: function (e) {
console.log('onError', JSON.parse(e));
},
onProgress: function (e) {
console.log('onProgress', e);
},
onComplete: function (e) {
console.log('onComplete', JSON.parse(e));
}
});
uploader.upload();
};
我有一个示例项目,其中包含一些代码(减去上传脚本):
【问题讨论】:
-
您可以在上传视频时查看您的控制台并向我们提供您收到的信息吗?应该有某种
response可以记录的信息,这些信息可以让我们更好地了解正在发生的事情。另外,在fileEntry.file中,尝试console.log使用file参数,看看你得到了什么信息。 Cordova 文件上传和文件系统导航可能很棘手。 -
在设备上,控制台日志未输出完整信息。环顾四周,似乎最好的选择可能是使用 FileTransfer 插件而不是编写单独的脚本来执行此操作:forums.meteor.com/t/file-uploads-via-cordova/2779/2
-
这是一个stackoverflow链接:stackoverflow.com/questions/29330360/…
标签: javascript file cordova video phonegap-plugins