不知道您是否找到了问题的解决方案,但也许这些代码会对您有所帮助。
function files() {
alert("Let's Start")
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function gotFS(fileSystem) {
fileSys = fileSystem;
}
, fsFail);
fileSys.root.getDirectory("myFolder/", {create: true, exclusive: false},
function(parent) {
folder=parent;
}
, dirFail);
//Creating a reader
var directoryReader = folder.createReader();
// Get a list of all the entries in the directory
directoryReader.readEntries(ReaderSucces,readerFail);
function ReaderSucces(entries){
alert("I'm reading")
var i,len;
len = entries.length;
for (i=0; i<len; i++) {
if (entries[i].isDirectory) {
var directoryReaderIn = entries[i].createReader();
directoryReaderIn.readEntries(ReaderSucces,readingFail);
}
if(entries[i].isFile==true)
{
entries[i].file(uploadFile, readingFail);
}
}
}
var fsFail = function(error) {
alert("failed with error code: " + error.code);
};
var dirFail = function(error) {
alert("Directory error code: " + error.code);
};
var readerFail = function(error) {
alert("Reading Directory error code: " + error.code);
};
var readingFail = function (error){
console.log("Reading Files error code: "+error.code);
};
}
function uploadFile(file) {
console.log("Let's upload!!!" + file)
var target="http://YOUR_IP/upload.php"; //the url to upload on server
var ft = new FileTransfer();
var path = file.fullPath; //"file://"+
var name = file.name;
var ft = new FileTransfer();
ft.upload(path, encodeURI(target), win, fail, { fileName: name, fileKey: "file", mimeType:"text/plain" });
// var ft = new FileTransfer();
//ft.upload(file.fullPath, target, win, fail, options);
}
服务器中的 PHP (upload.php)
<?php
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
} else {
echo "Upload: " . $_FILES["file"]["name"] . "\n " ;
echo "Type: " . $_FILES["file"]["type"] . "\n ";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb\n ";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<\n ";
if (file_exists("uploads/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $_FILES["file"]["name"]); //Save location
echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
}
}
?>
它将保存您文件夹中的所有文件(甚至是目录中的文件)。希望它有效。