[英]Not going through loop / After upload, not removing file
我的问题是如何在下面修复我的方法并了解为什么不起作用:
智能手机有10个音频文件。 LOOP FOR
遍历所有音频文件,然后ft.upload
。 这是错误的,做同样的第十个音频文件(最后一个音频文件)的十倍。 如何为LOOP FOR
遍历第一个文件,然后上传并删除它并使其循环播放,直到文件夹为空? 和entry.remove(success, fail);
我的文件成功上传后未删除文件。
任何建议都会有所帮助。
function uploadAudioFiles() {
var localFolder = "Sounds";
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) {
fs.root.getDirectory(localFolder, {create:false}, function(dirEntry){
var dirReader = dirEntry.createReader();
dirReader.readEntries(function(entries) {
for(var i = 0; i < entries.length; i++) {
console.log(entries);
var entry = entries[i];
//print all the audio 1 to 10.
var ft = new FileTransfer();
if (entry.isFile){
var path = entry.fullPath;
var name = entry.name;
//var reader = new FileReader();
//reader.readAsText(path);
//print 10x the same audio 10th on success.
ft.upload(path, "http://111.111.11.1:8080/hello/world/", function(result) {
console.log('Upload success: ' + result.responseCode);
console.log(result.bytesSent + ' bytes sent');
console.log("path:" + path);
//it calls success, but is not removing on my smartphone
fileSystem.root.getFile(path, {create: false, exclusive: false}, success, fail);
},
function(error) {
console.log('Error uploading file ' + path + ': ' + error.code);
},
{ fileName: name });
}
}
}, fail);
}, fail);
});
}
function success(entry) {
console.log("Removal succeeded" + entry.name + entry.fullPath + entry);
}
function fail(error) {
alert('Error removing file: ' + error.code);
}
尝试这个。 将您的上载调用包装在一个匿名函数中,该函数立即将条目作为参数执行。 这样,您可以“捕获”匿名函数中的当前条目值。 问题可能在于,由于条目的上传是在回调函数中完成的,因此条目的值在循环的每次迭代中都会被覆盖。 我以前也遇到过类似的问题,可以像这样解决。 我希望所有括号都正确。
if (entry.isFile){
var path = entry.fullPath;
var name = entry.name;
(function(currentPath, currentName) {
ft.upload(currentPath, "http://111.111.11.1:8080/hello/world/", function(result) {
console.log('Upload success: ' + result.responseCode);
console.log(result.bytesSent + ' bytes sent');
console.log("path:" + currentPath);
//it calls success, but is not removing on my smartphone
fileSystem.root.getFile(currentPath, {create: false, exclusive: false}, success, fail);
},
function(error) {
console.log('Error uploading file ' + currentPath + ': ' + error.code);
},
{ fileName: currentName });
})(path, name);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.