2012-06-18 35 views
14

我使用PhoneGap的文件API創建一個目錄,並創建中創建的目錄中的文件。該目錄正在創建,但文件沒有在目錄中創建。如何使用phonegap文件api在該目錄中創建一個目錄和文件?

我使用的代碼是:創建

document.addEventListener("deviceready", onDeviceReady, false); 

function onDeviceReady() { 
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); 
} 

function gotFS(fileSystem) { 
    var dataDir = fileSystem.root.getDirectory("data", {create: true}); 
    var file = dataDir.getFile("lockfile.txt", {create: true, exclusive: true}); 
} 

目錄數據,但不獲取創建lockfile.txt

回答

27

您需要在異步方式的代碼中調用:使用PhoneGap的

據工作3.0和高達iOS和Android從URL

document.addEventListener("deviceready", onDeviceReady, false); 

function onDeviceReady() { 
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); 
} 

function gotFS(fileSystem) { 
    fileSystem.root.getDirectory("data", {create: true}, gotDir); 
} 

function gotDir(dirEntry) { 
    dirEntry.getFile("lockfile.txt", {create: true, exclusive: true}, gotFile); 
} 

function gotFile(fileEntry) { 
    // Do something with fileEntry here 
} 
+0

感謝西蒙的偉大工程... – mmathan

+0

是這樣,沒有誰投下來這個答案要解釋它爲什麼不爲他們工作的人嗎? –

+0

我可以知道這個目錄的存儲位置嗎? – SSS

0

是這項工作?

var file = fileSystem.root.getFile("data" + "lockfile.txt", {create: true, exclusive: true}); 
+0

它創建文件,這個名字「datalockfile.txt」 – Blu

0

下載文件到您的設備

var folderName = 'xyz'; 
var fileName; 

function downloadFile(URL) { 
    //step to request a file system 
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fileSystemSuccess, fileSystemFail); 

    function fileSystemSuccess(fileSystem) { 
     var download_link = encodeURI(URL); 
     fileName = download_link.substr(download_link.lastIndexOf('/') + 1); //Get filename of URL 
     var directoryEntry = fileSystem.root; // to get root path of directory 
     directoryEntry.getDirectory(folderName, { 
      create: true, 
      exclusive: false 
     }, onDirectorySuccess, onDirectoryFail); // creating folder in sdcard 
     var rootdir = fileSystem.root; 
     var fp = fileSystem.root.toNativeURL(); // Returns Fullpath of local directory 

     fp = fp + "/" + folderName + "/" + fileName; // fullpath and name of the file which we want to give 
     // download function call 
     filetransfer(download_link, fp); 
    } 

    function onDirectorySuccess(parent) { 
     // Directory created successfuly 
    } 

    function onDirectoryFail(error) { 
     //Error while creating directory 
     alert("Unable to create new directory: " + error.code); 

    } 

    function fileSystemFail(evt) { 
     //Unable to access file system 
     alert(evt.target.error.code); 
    } 
} 

function filetransfer(download_link, fp) { 
    var fileTransfer = new FileTransfer(); 
    // File download function with URL and local path 
    fileTransfer.download(download_link, fp, 
     function(entry) { 
      alert("download complete: " + entry.fullPath); 
     }, 
     function(error) { 
      //Download abort errors or download failed errors 
      alert("download error source " + error.source); 
     } 
    ); 
} 
+0

它的工作對我來說... –

0
function download(URL, fileName){ 
    var folderName = 'xyz'; 
    var uri = encodeURI(URL); 

     window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, 
      function (fileSystem) { 
       var directoryEntry = fileSystem.root; // to get root path of directory 
       directoryEntry.getDirectory(folderName, { 
        create: true, 
        exclusive: false 
       }, onDirectorySuccess, onDirectoryFail); 
       var filename = fileSystem.root.toURL() + folderName + "/" + uri.substr(uri.lastIndexOf("/") + 1); 

       var fileTransfer = new FileTransfer(); 
       fileTransfer.download(uri, filename, 
        function(entry) { // download success 
         var path = entry.toURL(); //**THIS IS WHAT I NEED** 
         window.plugins.toast.showLongBottom("Download Completed: " + entry.fullPath, function (a) { 
         }, function (b) { 
         }); 
        }, 
        function(error) { 
         console.log("error") 
        } // irrelevant download error 
       );`enter code here` 
      }, 
      function(error) { 
       console.log("error2") 
      } // irrelevant request fileSystem error 
     ); 

     function onDirectorySuccess(parent) { 
      // Directory created successfuly 
      console.log("Directory created successfuly: " + JSON.stringify(parent)); 
      var fp = (parent.nativeURL) + fileName; 
      filetransfer(download_link, fp); 
     } 

     function onDirectoryFail(error) { 
      //Error while creating directory 
      alert("Unable to create new directory: " + error.code); 
     } 
    } 
+0

iOS和該下載代碼的Android工作既最新版本的cordova 3.4+全部以及離子框架 –

相關問題