我試過this,但它完全不滿足我的要求。我寫一個新問題:如何在Phonegap中創建嵌套目錄
var file_system;
var fs_root;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 1024*1024, onInitFs, request_FS_fail);
function onInitFs(fs) {
file_system= fs;
fs_root= file_system.root;
alert("ini fs");
create_Directory();
alert("ini fs done.");
}
var string_array;
var main_dir= "story_repository/"+ User_Editime;
string_array= new Array("story_repository/",main_dir, main_dir+"/rec", main_dir+"/img","story_repository/"+ User_Name);
function create_Directory(){
var start= 0;
var path="";
while(start < string_array.length) {
path = string_array[start];
alert(start+" th created directory " +" is "+ path);
fs_root.getDirectory(
path,
{create: true, exclusive: false},
function(entry) {
alert(path +"is created.");
},
create_dir_err()
);
start++;
}//while loop
}//create_Directory
function create_dir_err() {
alert("Recursively create directories error.");
}
function request_FS_fail() {
alert("Failed to request File System ");
}
雖然創建的目錄中,它發送我
ErrorCallback:「警報(」遞歸創建目錄錯誤「);」
首先,我不認爲這個代碼將工作,因爲我已經嘗試了這一點,這失敗:
window.requestFileSystem(
LocalFileSystem.PERSISTENT,
0,
//request file system success callback.
function(fileSys) {
fileSys.root.getDirectory(
"story_repository/"+ dir_name,
{create: true, exclusive: false},
//Create directory story_repository/Stallman_time.
function(directory) {
alert("Create directory: "+ "story_repository/"+ dir_name);
//create dir_name/img/
fileSys.root.getDirectory {
"story_repository/"+ dir_name + "/img/",
{create: true, exclusive: false},
function(directory) {
alert("Create a directory: "+ "story_repository/"+ dir_name + "/img/");
//check.
//create dir_name/rec/
fileSys.root.getDirectory {
"story_repository/"+ dir_name + "/rec/",
{create: true, exclusive: false},
function(directory) {
alert("Create a directory: "+ "story_repository/"+ dir_name + "/rec/");
//check.
//Go ahead.
},
createError
}
//create dir_name/rec/
},
createError
}
//create dir_name/img
},
createError
);
},
//Create directory story_repository/Stallman_time.
createError());
}
我只是一再只能打電話fs.root.getDirectory
但失敗了。但第一個幾乎是一樣的...
- 什麼問題呢?爲什麼第一個總是給我ErrorCallback?
- 爲什麼不能第二個工作?
- 有沒有人有更好的解決方案? (無ErrorcallBack味精)
ps:我在Android和PhoneGap 1.7.0上工作。
請在JavaScript中使用嘗試捕捉,然後讓我知道,如果我能幫助 – skhurams
嘗試此功能onSuccess(fileSystem){console.log(fileSystem.name); } //請求持久性文件系統 window.requestFileSystem(LocalFileSystem.PERSISTENT,0,onSuccess,onError); – skhurams
http://docs.phonegap.com/en/2.2.0/cordova_file_file.md.html#DirectoryEntry – skhurams