我正在使用 Cordova,并尝试在设备上的 SD 卡的根目录中创建一个文件夹。我使用以下代码创建文件夹并在其中添加文件“login.txt”:
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
function gotFS(fileSystem) {
fileSystem.root.getDirectory("citylook", {create: true}, gotDir);
}
function gotDir(dirEntry) {
dirEntry.getFile("login.txt", {create: true, exclusive: true}, gotFile);
}
function gotFile(fileEntry) {
// Do something with fileEntry here
fileEntry.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
writer.onwriteend = function(evt) {
console.log("contents of file now 'some sample text'");
writer.truncate(11);
writer.onwriteend = function(evt) {
writer.seek(0);
writer.write(testo);
writer.onwriteend = function(evt){
console.log("contents of file now '"+testo+"'");
}
};
};
writer.write("some sample text");
}
function fail(error) {
console.log(error.code);
}
现在,我需要在“citylook”文件夹中创建一个文件夹,所以我尝试了这个:
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onSuccess, null);
}
function onSuccess() {
fileSystem.root.getDirectory("citylook", {create: true, exclusive: false}, onGetDirectoryWin, onGetDirectoryFail);
fileSystem.root.getDirectory("citylook/nuovo", {create: true, exclusive: false}, onGetDirectoryWin2, onGetDirectoryFail2);
}
但我无法创建子文件夹。我的代码有什么问题?谢谢你。
解决了:
fileSystem.root.getDirectory("citylook", {create: true}, gotDir);
fileSystem.root.getDirectory("citylook/subfolder", {create: true}, gotDir);